ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Get range in list from combobox value (https://www.excelbanter.com/excel-programming/393732-get-range-list-combobox-value.html)

Axel

Get range in list from combobox value
 

I have a userform, with a combobox and two listboxes. I use this code to
get the sheets names from the workbook, to the combobox:
Private Sub UserForm_Initialize()
Dim ws As Integer
For ws = 1 To Sheets.Count
ComboBox1.AddItem Sheets(ws).Name
Next
End Sub
but I want to get range "B2:B50" in listbox1 from the sheet name, who is
selected with combobox1.
I have no ideas how to solve this, and the sheet names can be changed
from one day to another, so a case statement is no good here.
Anyone please!

*** Sent via Developersdex http://www.developersdex.com ***

joel

Get range in list from combobox value
 

Sub test()

For Each ws In Worksheets
ComboBox1.AddItem ws.Name
Next ws
Mysheet = ComboBox1.Text
With Sheets(Mysheet)
For Each cell In .Range("B2:B50")

ListBox1.AddItem cell
Next cell
End With
End Sub

"Axel" wrote:


I have a userform, with a combobox and two listboxes. I use this code to
get the sheets names from the workbook, to the combobox:
Private Sub UserForm_Initialize()
Dim ws As Integer
For ws = 1 To Sheets.Count
ComboBox1.AddItem Sheets(ws).Name
Next
End Sub
but I want to get range "B2:B50" in listbox1 from the sheet name, who is
selected with combobox1.
I have no ideas how to solve this, and the sheet names can be changed
from one day to another, so a case statement is no good here.
Anyone please!

*** Sent via Developersdex http://www.developersdex.com ***


Axel

Get range in list from combobox value
 

Thanks for answer!
I am still struggle with this.
am not sure that I dim the code correct.
Private Sub ComboBox1_Change()
Dim ws As Integer
Dim cell As range
For ws = 1 To Sheets.Count
ComboBox1.AddItem Sheets(ws).Name
Next
'ws returns sheet count? Combo returns sheet name?
ws = ComboBox1.Text
With Sheets(ws)
For Each cell In .range("B2:B50")

lbAvailableItems.AddItem cell
Next cell
End With
End Sub

Any suggestion?

*** Sent via Developersdex http://www.developersdex.com ***

Leith Ross[_2_]

Get range in list from combobox value
 
On Jul 19, 8:51 am, Axel wrote:
I have a userform, with a combobox and two listboxes. I use this code to
get the sheets names from the workbook, to the combobox:
Private Sub UserForm_Initialize()
Dim ws As Integer
For ws = 1 To Sheets.Count
ComboBox1.AddItem Sheets(ws).Name
Next
End Sub
but I want to get range "B2:B50" in listbox1 from the sheet name, who is
selected with combobox1.
I have no ideas how to solve this, and the sheet names can be changed
from one day to another, so a case statement is no good here.
Anyone please!

*** Sent via Developersdexhttp://www.developersdex.com***


Hello Axel,

To load ListBox1 when a selection is made in ComboBox1, you need to
have a macro in the Click event for the ComboBox.

Sub ComboBox1_Click()

Dim Cell As Range
Dim Wks As Worksheet

With ComboBox1
If .ListIndex = -1 Then Exit Sub
Wks = .List(.ListIndex)
End With

With ListBox1
.Clear
For Each Cell In Wks.Range("B2:B50")
.AddItem Cell.Value
Next Cell
End With

End Sub

Sincerely,
Leith Ross


Axel

Get range in list from combobox value
 

Thank you very much!
Axel

*** Sent via Developersdex http://www.developersdex.com ***


All times are GMT +1. The time now is 11:51 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com