VBA combo box - navigate to multiple different sheets based on drop down selection

Mariyka1

New Member
Joined
Nov 1, 2016
Messages
44
Hello, I have 7 different sheets that I want the combo drop down box to open based on the number in cell C50 in a separate control sheet. I have the following code written although keep getting an error "Compile error: Block If without End If"

any help is greatly appreciated!



Sub DropDown4_Change()


Dim intType As Integer
intType = Range("C50").Value
If intType = 2 Then
Sheets("Sheet2").Visible = True
Sheets("Sheet2").Select
Range("A1").Select
If intType = 3 Then
Sheets("Sheet3").Visible = True
Sheets("Sheet3").Select
Range("A1").Select
If intType = 4 Then
Sheets("Sheet4").Visible = True
Sheets("Sheet4").Select
Range("A1").Select
If intType = 5 Then
Sheets("Sheet5").Visible = True
Sheets("Sheet5").Select
Range("A1").Select
If intType = 6 Then
Sheets("Sheet6").Visible = True
Sheets("Sheet6").Select
Range("A1").Select
If intType = 7 Then
Sheets("Sheet7").Visible = True
Sheets("Sheet7").Select
Range("A1").Select
ElseIf intType = 8 Then
Sheets("Sheet8").Visible = True
Sheets("Sheet8").Select
Range("A1").Select


End If



End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Does this work?

Code:
Sub test()
Dim ws As String: ws = "Sheet" & Range("C50").Value

Sheets(ws).Visible = True
Sheets(ws).Select
Sheets(ws).Range("A1").Select

End Sub
 
Last edited:
Upvote 0
Does this work?

Code:
Sub test()
Dim ws As String: ws = "Sheet" & Range("C50").Value

Sheets(ws).Visible = True
Sheets(ws).Select
Sheets(ws).Range("A1").Select

End Sub

No unfortunately this code did not work received subscript our of range message
 
Upvote 0
What do you have in cell C50? That error means that it's looking for a sheet named Sheet#, where # is whatever is in cell C50. So, I have 2 in cell C50, and the code finds 'Sheet2', makes it visible and activates it.
 
Upvote 0
You can try this as well.

Code:
Sub testII()
Dim intType As Integer: intType = Range("C50").Value
Dim ws As Worksheet

Select Case intType
    Case 2
        Set ws = Sheets("Sheet2")
    Case 3
        Set ws = Sheets("Sheet3")
    Case 4
        Set ws = Sheets("Sheet4")
    Case 5
        Set ws = Sheets("Sheet5")
    Case 6
        Set ws = Sheets("Sheet6")
    Case 7
        Set ws = Sheets("Sheet7")
    Case 8
        Set ws = Sheets("Sheet8")
End Select

ws.Visible = True
ws.Select
Range("A1").Select
    
End Sub
 
Upvote 0
You can try this as well.

Code:
Sub testII()
Dim intType As Integer: intType = Range("C50").Value
Dim ws As Worksheet

Select Case intType
    Case 2
        Set ws = Sheets("Sheet2")
    Case 3
        Set ws = Sheets("Sheet3")
    Case 4
        Set ws = Sheets("Sheet4")
    Case 5
        Set ws = Sheets("Sheet5")
    Case 6
        Set ws = Sheets("Sheet6")
    Case 7
        Set ws = Sheets("Sheet7")
    Case 8
        Set ws = Sheets("Sheet8")
End Select

ws.Visible = True
ws.Select
Range("A1").Select
    
End Sub

This worked - thank you very much!!
 
Upvote 0

Forum statistics

Threads
1,214,570
Messages
6,120,294
Members
448,953
Latest member
Dutchie_1

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