View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Leith Ross[_2_] Leith Ross[_2_] is offline
external usenet poster
 
Posts: 128
Default 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