ListBox Source in Userform

johnpants

New Member
Joined
Oct 31, 2005
Messages
44
Hi, I am creating a sales forecasting tool which will be updated regularly and am using UserForms to enter the data. I have the form created and everything is working correctly, apart from I cannot link the Listbox's to the data on the spreadsheet. I haven't names the ranges as from what I find, if someone was to add another record to the range, it would not be included in the range without manually renaming it.

The source data is located on a seperate excel file in the same folder.

File=Admin
Worksheet=Data
Range=E5:E200
Userform=SalesEntry

could anyone tell me the required code to get this data to appear in my ListBox please? Would be much appreciated.

Thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Ok, I have just found this code that is supposed to do exactly what I want, but it brings up a runtime error when I test it... Does anyone know where I am going wrong? :(

Private Sub SalesEntry_Initialize()
Dim ListItems As Variant, i As Integer
Dim SourceWB As Workbook
With Me.ListBox1
.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("D:\Work\DrFoster\Admin.xls", _
False, True)
ListItems = SourceWB.Worksheets(Data).Range("E5:E200").Value
' get the values you want
SourceWB.Close False ' close the source workbook without saving changes
Set SourceWB = Nothing
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
Application.ScreenUpdating = True
End With
End Sub

I've coloured the sections that the error selects. Also, I have created this in a new Module. I am fairly new to this so please bare with me if it is a blatant mistake.. :)

Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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