Using last row in this code

wlknspc7

New Member
Joined
May 12, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello, I have an excel database with about 50 unique sheets. The formatting of these sheets is the same in all cases but each worksheet is for a different supplier, some worksheets contain 10 rows, some 450+
I made an userform, to make it easier to add, edit, etc. the products listed on these sheets.
This userform has a listbox that displays the content of these worksheets and I have a combobox that contains the names of the worksheets so I just have to click on the suppliers name on the combobox and it shows the proper worksheet I'm looking for
The problem is, using this range "!A5:P10000" does the job and shows the correct data from the worksheet but it also fills my listbox with endless blank rows after the rows with data, and no matter how I try I can't implement a lastrow range into RowSource
Is it possible to use a dynamic range here?

My code:

Private Sub ComboBox1_Click()
Dim ws As Worksheet

Set ws = Worksheets(Me.ComboBox1.Value)

With Me.ListBox1
.ColumnHeads = True
.RowSource = "A4:P4"
.ColumnCount = 16
.ColumnWidths = "20,0,0,60,120,120,200,40,40,40,40,40,40,40,40,120"
.RowSource = ws.Name & "!A5:P10000"
End With

End Sub

Thanks for the help in advance!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
How about
VBA Code:
.RowSource = ws.Range("A5:P" & ws.Range("A" & Rows.Count).End(xlUp).Row).Address(, , , 1)
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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