Repeat VBA Function for Separate Events in Same Worksheet

mlp1415

New Member
Joined
Dec 3, 2015
Messages
1
Hi all,

I am rather new to Visual Basic, but I am trying to duplicate a function that I have wherein if an amount in cell N165 is greater than 5000, then the two rows below the cell (in this case, rows 166 and 167) will become visible. I am trying to do this in multiple separate instances on the same worksheet (with an amount greater than 5000 in N168 revealing 169 and 170, N171 revealing 172 and 173, etc.) Is there a way to accomplish this? Any help would be greatly appreciated!

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target.Address = "$N$165" Then Exit Sub
If Not IsNumeric(Target) Then Exit Sub
If Target > 5000 Then
Range("166:167").EntireRow.Hidden = False
Else
Range("166:167").EntireRow.Hidden = True
End If
If Not Target.Address = "$N$168" Then Exit Sub
If Not IsNumeric(Target) Then Exit Sub
If Target > 5000 Then
Range("169:170").EntireRow.Hidden = False
Else
Range("169:170").EntireRow.Hidden = True
End If
If Not Target.Address = "$N$171" Then Exit Sub
If Not IsNumeric(Target) Then Exit Sub
If Target > 5000 Then
Range("172:173").EntireRow.Hidden = False
Else
Range("172:173").EntireRow.Hidden = True
EndIf



End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Not sure if this is what you want, but give it a try.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ary As Variant
If Not Intersect(Target, Range("N165, N168, N169")) Is Nothing Then
    ary = Array(Range("N165"), Range("N168"), Range("N171"))
    For i = LBound(ary) To UBound(ary)
        If ary(i).Value > 5000 Then
            ary(i).Offset(1, 0).Resize(2, 1).EntireRow.Hidden = False
        Else
            ary(i).Offset(1, 0).Resize(2, 1).EntireRow.Hidden = True
        End If
    Next
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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