Multiple If statements

MrMadHatter

New Member
Joined
Nov 19, 2016
Messages
5
Hello,

I have the following VBA code that displays a specific row based on a value selected from a drop-down (otherwise it's hidden):

Code:
    If Target.Address = "$B$9" Then
        Range("26:26").EntireRow.Hidden = True
    If Target = "Yes" Then
        Range("26:26").EntireRow.Hidden = False
        End If
    End If

That basically displays row 26 when the user selects 'Yes' in B9 but keeps it hidden if the user selects 'No'. I now need to extend this code to consider another cell and display an additional row.

So, if the user selects 'Yes' in B9 then I need the code to reference cell B6 to see what value the user has selected there and then show one of two additional rows (40 or 41), e.g. if the user has selected '1' in B6 then show row 40/hide row 41, or if the user has selected '2' in B6 then hide row 40/display row 41.

To sum up, here are the possible scenarios:

User selects 'Yes' in B9 and '1' in B6 - Show rows 26 and 40 (hide row 41)
User selects 'Yes' in B9 and '2' in B6 - Show rows 26 and 41 (hide row 40)
User selects 'No' in B9 and either '1' or '2' in B6 - Hide rows 26, 40 and 41

I'm pretty new to VBA so I was hoping somebody could enlighten me as to the general syntax that's required here. I'm assuming I need to incorporate the And function?

I hope this is clear and thanks for any help on this :)
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hello,

I have the following VBA code that displays a specific row based on a value selected from a drop-down (otherwise it's hidden):

Code:
    If Target.Address = "$B$9" Then
        Range("26:26").EntireRow.Hidden = True
    If Target = "Yes" Then
        Range("26:26").EntireRow.Hidden = False
        End If
    End If

That basically displays row 26 when the user selects 'Yes' in B9 but keeps it hidden if the user selects 'No'. I now need to extend this code to consider another cell and display an additional row.

So, if the user selects 'Yes' in B9 then I need the code to reference cell B6 to see what value the user has selected there and then show one of two additional rows (40 or 41), e.g. if the user has selected '1' in B6 then show row 40/hide row 41, or if the user has selected '2' in B6 then hide row 40/display row 41.

To sum up, here are the possible scenarios:

User selects 'Yes' in B9 and '1' in B6 - Show rows 26 and 40 (hide row 41)
User selects 'Yes' in B9 and '2' in B6 - Show rows 26 and 41 (hide row 40)
User selects 'No' in B9 and either '1' or '2' in B6 - Hide rows 26, 40 and 41

I'm pretty new to VBA so I was hoping somebody could enlighten me as to the general syntax that's required here. I'm assuming I need to incorporate the And function?

I hope this is clear and thanks for any help on this :)

try this

Code:
    If Target.Address = "$B$9" Then
        Range("26:26").EntireRow.Hidden = True
        Range("40:40").EntireRow.Hidden = True
        Range("41:41").EntireRow.Hidden = True
        If Target = "Yes" And ActiveSheet.Range("B6") = "1" Then
            Range("26:26").EntireRow.Hidden = False
            Range("40:40").EntireRow.Hidden = False
        ElseIf Target = "Yes" And ActiveSheet.Range("B6") = "2" Then
            Range("26:26").EntireRow.Hidden = False
            Range("41:41").EntireRow.Hidden = False
        End If
    End If
 
Upvote 0
Thank you so much for that solution RCBricker, it works perfectly! I just couldn't get my head around it but that solution is really tidy!
 
Upvote 0

Forum statistics

Threads
1,214,521
Messages
6,120,018
Members
448,937
Latest member
BeerMan23

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