Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

show data in listbox based on sheet name in combobox

0

Hello ,

I would show data in listbox based on selected sheet from combobox

my project doesn't show anything !

Dim r As Variant

Private Sub ComboBox1_Change()
 Call LoadData
End Sub
 Sub LoadData()
On Error Resume Next
    Set sh = Sheets(ComboBox1.Value)
    On Error GoTo 0
    If sh Is Nothing Then Exit Sub
r = Sheets(sh.Name).Range("A1").CurrentRegion.Value
End Sub
Private Sub UserForm_Initialize()
Dim sh As Variant
For Each sh In Sheets(Array("MK", "MT"))
ComboBox1.AddItem sh.Name
Next sh
End Sub

I hope somebody help.

Answer
Discuss

Answers

0
Selected Answer

Malkal

Sorry but I don't have time to give a full answer but your routine LoadData uses the line:

r = Sheets(sh.Name).Range("A1").CurrentRegion.Value

to succesfully load the data form a chosen sheet into an array r.

You'll need to loop through that array and add list items to ListBox1  if you want to see that data in your UserForm.

Revision #1, 28 December 2024

If you don't need to exclude the Totals row, you can just add the code in bold below (with comments and included in the attached, revised file):

Sub LoadData()
    On Error Resume Next
    Set sh = Sheets(ComboBox1.Value)
    On Error GoTo 0
    If sh Is Nothing Then Exit Sub

    ' get data from sheet
    r = Sheets(sh.Name).Range("A1").CurrentRegion.Value

    ' populate the listbox
    With Me.ListBox1
        'set columns to match data width
        .ColumnCount = UBound(r, 2)
        ' add data
        .List = r
    End With

End Sub

Note that in the UserForm design for ListBox1, I set the number of columns to 8 (normally, via the ColumnCount property) but the code above adjusts that property automatically, just in case you have fewer or more columns on one sheet for example.

Likewise I made the second column INV NO (typo now corrected) 50% wider by making the ColumnWidths property:

50,75,50,50,50,50,50,50

(which VBA adjusts).

Hope this helps.

Discuss

Discussion

Hi john,
I try with this
For i = LBound(sh) To UBound(sh)
    r = Sheets(sh.Name(i)).Range("A1").CurrentRegion.Value
Next i

but  show mismatch error
For i = LBound(sh) To UBound(sh)
Malkal (rep: 26) Dec 24, '24 at 9:20 am
Malkal, That won't work because sh (the ComboBox1 value) is a single string (not an array so has no upper and lower bounds) but please see Revision #1, 28 December 2024 to my Answer (plus a revised file)
John_Ru (rep: 6607) Dec 28, '24 at 11:25 am
Also, the title of your Question ends (somewhat confusingly) with the word "comb", probably owing to a limit on that field. To help other users, please edit that title to omit "name" and end with "ComboBox". Thanks in advance. 
John_Ru (rep: 6607) Dec 28, '24 at 6:37 pm
thanks John for solution.
EDITED: title
Malkal (rep: 26) Dec 29, '24 at 3:04 am
Glad that helped and thanks for selecting my Answer, Maklal. Also thank you for editing the Question title. 
John_Ru (rep: 6607) Dec 29, '24 at 6:35 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login