Select a worksheet based on excel drop down

levans_1986

New Member
Joined
Aug 9, 2017
Messages
13
Hello

I am creating a questionnaire which comprises of three excel worksheets. On the first worksheet one of the questions has a yes/no answer in a dropdown. I want to automatically select a worksheet depending on the answer to the question

For example:
If someone selects yes they are automatically taken to worksheet 2(PartA). If they select no they are automatcially taken to worksheet 3(PartB).

I have recorded two macros, Mac_PartA which takes you to sheet 2 and Mac_PartB which takes you to sheet 3. I've tried to use some vba code but I'm not overly familar with it and I am getting a compile error. the code I have is saved in the workbook rather than the sheet

the code is
Private Sub Workbook_SheetChange(ByVal Target As Range)
If Not Intersect(Target, Range("P2")) Is Nothing Then Select Case Range("P2")

Select Case Range("P2")

Case "Yes": Mac_PartA
Case "No": Mac_PartB
End Select
End If
End Sub

Can anyone help?

Many thanks in advance :)

Laura
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try putting your VBA code in a Worksheet_Change event procedure (which runs upon a change to a cell, like P2) instead of a Worksheet_SheetChange event procedure.
 
Upvote 0
You had two Select Case statements, one in-line with the If-Then statement, one on the next line.

Try:

Code:
Private Sub Workbook_SheetChange(ByVal Target As Range)
If Not Intersect(Target, Range("P2")) Is Nothing Then
    Select Case Range("P2")
        Case "Yes": Mac_PartA
        Case "No": Mac_PartB
    End Select
End If
End Sub
 
Upvote 0
In the VB Editor go to the VB module of the sheet that this drop-down box appears in.
Then, above the VB Editor, you will see two drop down boxes.
From the one of the left, select "Worksheet"
From the one on the right, select "Change"

This will give you the shell of the Worksheet_Change event procedure, which looks like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


End Sub
Just take the body of the VB code from what you already have (everything but the first and last lines), and paste it in between the two lines shown above.
 
Upvote 0
Thank you so much that works! :) :) However only other issue is I want this macro to trigger if any cell in column p after p1 changes. at the moment it only triggers when p2 changes. how do I get the whole column to change?

Thanks again your help is much appreciated :)
 
Upvote 0
I would make a few minor modifications like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


'   Exit if more than one cell updated at a time
    If Target.Count > 0 Then Exit Sub
    
'   Only run if a cell in column P after row 1 is updated
    If Target.Column = 16 And Target.Row > 1 Then
        Select Case Target.Value
            Case "Yes": Mac_PartA
            Case "No": Mac_PartB
        End Select
    End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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