Listbox Displays Odd Data On First Use Then Data as Expected

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I have a UserForm that Loads and Shows when the Workbook is opened.
The User is given the current date setting that is saved to a worksheet cell on each use.
Ths User is then given a choice to use a different date Yes or No.
If the User chooses Yes, then a Listbox on the UserForm is populated with a list of dates from an Excel Table.

This is where I see some strange results.
One time nothing was displayed.
Another time, dates were dispalyed, but they were not in the correct order as the Excel Table.
It seems like a wild guess when I close the workbook, re-open the workbook and click Yes.

However, if I subsequently raise the form, the dates are displayed in the Listbox as expected.

Have you seen this behavior before?
Any ideas how to defeat this?

Thanks,
-w


VBA Code:
Private Sub cmdYes_Click()

'Get user selected values from listbox

'objects
    Dim wb As Workbook
    Dim wsCriteria As Worksheet
    Dim lo As ListObject
    Dim rng As Range
    
    Set wb = ThisWorkbook
    Set wsCriteria = wb.Worksheets("Criteria")
    Set lo = wsCriteria.ListObjects("tblDates")
    Set rng = lo.ListColumns(2).Range.Offset(1, 0)
    Set rng = rng.Resize(rng.Rows.Count - 1, 1)
    Debug.Print rng.Address

'Populate combo box on user form
    With Me
        .lstReportDates.RowSource = rng.Address
    End With

'tidy up
    Set rng = Nothing
    Set lo = Nothing
    Set ws = Nothing
    Set wb = Nothing

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi
see if this update to your code resolves the issue

VBA Code:
Private Sub cmdYes_Click()
    
    'Get user selected values from listbox
    
    'objects
    Dim wb          As Workbook
    Dim wsCriteria  As Worksheet
    Dim lo          As ListObject
    Dim rng         As Range
    
    Set wb = ThisWorkbook
    Set wsCriteria = wb.Worksheets("Criteria")
    Set lo = wsCriteria.ListObjects("tblDates")
    
    Set rng = lo.ListColumns(2).DataBodyRange
    
    Debug.Print rng.Address
    
    'Populate combo box on user form
    With Me.lstReportDates
        .ColumnHeads = True
        .RowSource = "'" & wsCriteria.Name & "'!" & rng.Address
    End With
    
    'tidy up
    Set rng = Nothing
    Set lo = Nothing
    Set ws = Nothing
    Set wb = Nothing
    
End Sub

Dave
 
Upvote 0
Thanks Dave,

I only changed .ColumnHeads = False.

Looks good so far, I'll keep an eye on it.

Thanks
-w
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top