Script not executing

Kafo

New Member
Joined
Jul 20, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello, am new to VBA and am struggling with a code that isn't running as expected.
This is what I want to achieve.
a) If one selects "Yes" in cell C8 of worksheet "summary", then rows (8:13) in worksheets "financials" & "ROI" are hidden
b) If one selects "No" in cell C8 of worksheet "summary", then rows (8:13) in worksheets "financials" & "ROI" are unhidden
c) If one selects "Yes" in cell C9 of worksheet "summary", then rows (30:50) in worksheets "financials" & "ROI" are hidden
d) If one selects "No" in cell C9 of worksheet "summary", then rows (30:50) in worksheets "financials" & "ROI" are unhidden

This is the code i wrote....not sure why its not working.....someone please help :)

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address <> "$C$9" Then Exit Sub
With Sheets("Financials").Rows("30:50").EntireRow
If Target.Value = "Yes" Then
.Hidden = True
Else
.Hidden = False
End If
End With

If Target.Address <> "$C$9" Then Exit Sub
With Sheets("ROI").Rows("28:45").EntireRow
If Target.Value = "No" Then
.Hidden = True
Else
.Hidden = False
End If
End With

If Target.Address <> "$C$8" Then Exit Sub
With Sheets("Financials").Rows("8:13").EntireRow
If Target.Value = "Yes" Then
.Hidden = True
Else
.Hidden = False
End If
End With

If Target.Address <> "$C$8" Then Exit Sub
With Sheets("ROI").Rows("8:13").EntireRow
If Target.Value = "No" Then
.Hidden = True
Else
.Hidden = False
End If
End With

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the Board!

The issue is with lines like this one near the top of your code:
VBA Code:
If Target.Address <> "$C$9" Then Exit Sub
If you update cell C8, it will never get to that part of the code because the line shown above is telling it to exit the sub before it ever gets to that part of the code!

Try something like this instead:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count > 1 Then Exit Sub

Select Case Target.Address
    
    Case "$C$9"
        With Sheets("Financials").Rows("30:50").EntireRow
            If Target.Value = "Yes" Then
                .Hidden = True
            Else
                .Hidden = False
            End If
        End With
    
        With Sheets("ROI").Rows("28:45").EntireRow
            If Target.Value = "No" Then
                .Hidden = True
            Else
                .Hidden = False
            End If
    End With

    Case "$C$8"
        With Sheets("Financials").Rows("8:13").EntireRow
            If Target.Value = "Yes" Then
                .Hidden = True
            Else
                .Hidden = False
            End If
        End With

        With Sheets("ROI").Rows("8:13").EntireRow
            If Target.Value = "No" Then
                .Hidden = True
            Else
                .Hidden = False
            End If
        End With

    End Select
    
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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