Someone helped me with the code below and it worked while I had it placed in a spreadsheet. I tried moving it to a standard module and it will no longer work tells me invalid use of Me. How can I populate these two combo boxes at start up?
Dim ListItems As Variant, i As Integer
Dim SourceWB As Workbook
With Me.ComboBox1
.Clear ' remove existing entries from the listbox
' turn screen updating off,
' prevent the user from seeing the source workbook being opened
Application.ScreenUpdating = False
' open the source workbook as ReadOnly
Set SourceWB = Workbooks.Open("U:\DOH.xls", _
False, True)
ListItems = SourceWB.Worksheets(1).Range("A2:A150").Value
' get the values you want
SourceWB.Close False ' close the source workbook without saving changes
Set SourceWB = Nothing
Application.ScreenUpdating = True
ListItems = Application.WorksheetFunction.Transpose(ListItems)
' convert values to a vertical array
For i = 1 To UBound(ListItems)
.AddItem ListItems(i) ' populate the listbox
Next i
.ListIndex = -1 ' no items selected, set to 0 to select the first item
End With
Dim ListItems2 As Variant, i2 As Integer
Dim SourceWB2 As Workbook
With Me.ComboBox2
.Clear ' remove existing entries from the listbox
' turn screen updating off,
' prevent the user from seeing the source workbook being opened
Application.ScreenUpdating = False
' open the source workbook as ReadOnly
Set SourceWB2 = Workbooks.Open("U:\DOH.xls", _
False, True)
ListItems2 = SourceWB2.Worksheets(1).Range("d2:D62").Value
' get the values you want
SourceWB2.Close False ' close the source workbook without saving changes
Set SourceWB2 = Nothing
Application.ScreenUpdating = True
ListItems2 = Application.WorksheetFunction.Transpose(ListItems2)
' convert values to a vertical array
For i2 = 1 To UBound(ListItems2)
.AddItem ListItems2(i2) ' populate the listbox
Next i2
.ListIndex = -1 ' no items selected, set to 0 to select the first item
End With
End Sub
Bookmarks