Listbox source

mrmrf

New Member
Joined
Jun 3, 2019
Messages
34
Hi.

I am having trouble showing source data in a listbox.

I use the below code to enter data from the userform to a worksheet, then the last two lines of code displays all data within a listbox. And this works ok.

But on first open of the userform i want the listbox to show the data already entered without needing to add more and using the code below.

I have edited the rowsource of the listbox to be 'ASamples'!A1:L6535 but this only shows data in the listbox from column 0, and doesnt show the other columns.

Any tips?

Thanks.

Code:
Private Sub CommandButtonAddSample_Click()
Dim wks As Worksheet
Dim AddNew As Range
Set wks = Sheets("ASamples")
Set AddNew = wks.Range("A65356").End(xlUp).Offset(1, 0)
AddNew.Offset(0, 0).Value = txtA1.Text
AddNew.Offset(0, 1).Value = txtA2.Text
AddNew.Offset(0, 2).Value = txtA3.Text
AddNew.Offset(0, 3).Value = txtA4.Text
AddNew.Offset(0, 4).Value = txtA5.Text
AddNew.Offset(0, 9).Value = txtA6.Text
AddNew.Offset(0, 10).Value = txtA7.Text

lstDisplay.ColumnCount = 12
lstDisplay.RowSource = "'ASamples'!A1:L65356"
End Sub

<strike></strike>
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
But on first open of the userform i want the listbox to show the data already entered without needing to add more and using the code below.

Using code ...
Code:
Private Sub UserForm_Initialize()
    lstDisplay.ColumnCount = [B]12[/B]

[COLOR=#006400]'avoid putting so many rows into the listbox[/COLOR]
    With Sheets("ASamples")
[COLOR=#006400]        'if a contiguous range[/COLOR]
        lstDisplay.RowSource = .Range("A1").CurrentRegion.Resize(, 12).Address
[COLOR=#006400]        'OR assuming column A contains a value in every row[/COLOR]
        lstDisplay.RowSource = .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 12).Address
    End With
End Sub

Use the Properties window ...
- amend the listbox Column Count property to 12
 
Last edited:
Upvote 0
It nearly always is ! ;)
thanks for the feedback
:beerchug:
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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