PDA

View Full Version : Opening A Sheet From A Combobox



satyampakker
06-05-2008, 08:04 AM
In the combox box i am genrating the sheet names of the workbook . i want a code such that if i select the sheet name it should open the sheet selected in that workbook .

this is the code for genrating sheetnames in the combobox



Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
ComboBox1.AddItem ws.Name
Next


thanks
satyam

mikerickson
06-05-2008, 08:18 AM
If the combobox is in a userform, this should do it.
With Userform1.ComboBox1.
If 0 <= .ListIndex then
ThisWorkbook.Sheets(.Value).Activate
End If
End With

lifeson
06-05-2008, 08:18 AM
Try this
Private Sub ComboBox1_Change()
Dim ws As String

ws = ComboBox1.Value
Sheets(ws).Select

End Sub

satyampakker
06-05-2008, 08:29 AM
Try this
Private Sub ComboBox1_Change()
Dim ws As String

ws = ComboBox1.Value
Sheets(ws).Select

End Sub


thank you very much the code is working !!

i have another question now i am able to open the work sheet but i am having a list box which is presently getting its value from the column 1 of sheet1 like i need to update my list box with column1 of the sheet which is selected from the combobox

this is the code which i am using to intialize the form



Private Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
ComboBox1.AddItem ws.Name
Next

ListBox1.List = Range(Cells(2, 1), Cells(2, 1).End(xlDown)).Value

End Sub



thanks
satyam

satyampakker
06-05-2008, 08:42 AM
hey thanks i solved my problem by using this code:



Private Sub ComboBox1_Change()
Dim ws As String

ws = ComboBox1.Value
Sheets(ws).Select
ListBox1.List = Range(Cells(2, 1), Cells(2, 1).End(xlDown)).Value
End Sub