Userform and retrieving data from listboxes

erinbe

New Member
Joined
Sep 27, 2011
Messages
21
Hello,

I've got a user form with 2 listboxes - The first box populates a listing of names of worksheets. When clicking on one name then a button I would like to populate the data from that sheet into a second list box (about 10 rows and 12 columns of data only)

When changing the name of listbox1 then clicking show I'd like that data to change...

I'm new and can't seem to figure this out - I'm sure its an easy syntax that I can't quite get...Please help... This is what I have so far...


Code:
Private Sub UserForm_Initialize()
   
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    If ws.Visible = xlSheetVisible Then
        ListBox1.AddItem ws.Name
    End If
Next ws

TextBox1 = ListBox1.Value
    
End Sub

Private Sub ListBox1_Click()

Label1.Caption = Sheets(ListBox1.Value).Range("A1").Value

End Sub


Private Sub CommandButton1_Click()
 Dim ws As Worksheet

    With ListBox2
    Sheets(ListBox1.Value).Select  [B][SIZE="4"] (is this line right?)
   
(where do I go from here?)[/SIZE][/B]
        


    End With
End Sub
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
This worked for me:

Code:
Private Sub CommandButton1_Click()
    ListBox2.List = Worksheets(ListBox1.Value).Range("A1:T10").Value
End Sub
 
Upvote 0
That's great and so easy ! Thank You...

Is there a way to format the data I'm posting? Some are percentages and times...but they all display as decimal numbers.
 
Upvote 0
Try:

Code:
Private Sub CommandButton1_Click()
    Dim r As Long
    Dim c As Long
    With Worksheets(ListBox1.Value).Range("A1:T10")
        ListBox2.List = .Value
        For r = 1 To .Rows.Count
            For c = 1 To .Columns.Count
                ListBox2.List(r - 1, c - 1) = .Cells(r, c).Text
            Next c
        Next r
    End With
End Sub
 
Upvote 0
That is great! Thank you so much!!

Why does this work and not the previous? Because it's taking the actual value of each cell?
 
Upvote 0
The Text property returns the value as it appears in the cell. The Value property returns the unformatted value.
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

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