Read data from worksheet based on selection from ListBox

abukarim

New Member
Joined
Feb 2, 2015
Messages
4
Hi All,

I am very new to VBA and I need your help on this code. My userform has a ListBox that gets populated with text. Each text string is a name of worksheet in the same workbook. The idea of this code is that when I highlight one or more of text string at the ListBox, a command button is supposed to look at the each of these worksheet name and read the data range from cell A1 to last string in column A and go to the next sheet and so on. Below is my code. I ran the code and I am getting an error at "Set ws.Name = ListBox1.Selected(i)". Can you look at the code and let me know what I am doing wrong.
Thanks
-----------------------------
Private Sub AvailableServices_Click()
Dim cell As Range
Dim rng As Range
Dim ws As Worksheet
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Set ws.Name = ListBox1.Selected(i)
Set rng = ws.Range("A1:")

For Each cell In rng.Cells
Me.ListBox2.AddItem cell.Value
Next cell
End If
Next i
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi All,

I am very new to VBA and I need your help on this code. My userform has a ListBox that gets populated with text. Each text string is a name of worksheet in the same workbook. The idea of this code is that when I highlight one or more of text string at the ListBox, a command button is supposed to look at the each of these worksheet name and read the data range from cell A1 to last string in column A and go to the next sheet and so on. Below is my code. I ran the code and I am getting an error at "Set ws.Name = ListBox1.Selected(i)". Can you look at the code and let me know what I am doing wrong.
Thanks
-----------------------------
Private Sub AvailableServices_Click()
Dim cell As Range
Dim rng As Range
Dim ws As Worksheet
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Set ws.Name = ListBox1.Selected(i)
Set rng = ws.Range("A1:")

For Each cell In rng.Cells
Me.ListBox2.AddItem cell.Value
Next cell
End If
Next i
End Sub

can you try this
Code:
Set ws = ListBox1.Selected(i)

and change
Code:
Set rng = ws.Range("A1:")

to
Code:
Set rng = ws.Range("A:A")

also your list box 2 will contain entries for blank cells. so you need to use worksheetfunction.counta method to get only non blank cells in your listbox
 
Last edited:
Upvote 0
can you try this
Code:
Set ws = ListBox1.Selected(i)

and change
Code:
Set rng = ws.Range("A1:")

to
Code:
Set rng = ws.Range("A:A")

also your list box 2 will contain entries for blank cells. so you need to use worksheetfunction.counta method to get only non blank cells in your listbox

Thanks Jumbledore.
I tried your suggestion but now I am getting a different error "Compile error: Type mismatch" and .Selected(i) is highlighed. Any reason why?
 
Upvote 0
Thanks Jumbledore.
I tried your suggestion but now I am getting a different error "Compile error: Type mismatch" and .Selected(i) is highlighed. Any reason why?

sorry change
Code:
[COLOR=#574123]Set ws = ListBox1.Selected(i)
to [/COLOR]
Code:
[COLOR=#574123]Set ws = ListBox1.[/COLOR][COLOR=#333333]Column[/COLOR][COLOR=#574123](i)[/COLOR]
 
Upvote 0
sorry change
Code:
[COLOR=#574123]Set ws = ListBox1.Selected(i)[/COLOR]
to
Code:
[COLOR=#574123]Set ws = ListBox1.[/COLOR][COLOR=#333333]Column[/COLOR][COLOR=#574123](i)[/COLOR]

I really appreciate your help here. Ok, so I tried your last suggestion and now I am getting a different error message. The error message is "Compile error: Object required" and the ws is highlighted on the code. What will be the object in this case for ws? Any idea
 
Upvote 0
I really appreciate your help here. Ok, so I tried your last suggestion and now I am getting a different error message. The error message is "Compile error: Object required" and the ws is highlighted on the code. What will be the object in this case for ws? Any idea

Sorry the error message that I sent you is not the right one. The error message after I changed the ListBox1.column(i) is "Run-time error '381': Could not get the column property. Invalid property array index"
 
Upvote 0
Sorry the error message that I sent you is not the right one. The error message after I changed the ListBox1.column(i) is "Run-time error '381': Could not get the column property. Invalid property array index"

sorry instead of

Code:
Set ws = ListBox1.Column(i)
use


Code:
Set ws = ListBox1.List(i)

can you please upload your workbook?
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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