worksheet change event for multiple cells

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all,

I have this code:
Code:
 Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [H16]) Is Nothing Then
    If [H16] < -9.99 Then
        [I1].EntireColumn.Hidden = False
    Else: [I1].EntireColumn.Hidden = True
    End If
End If
End Sub

It works perfectly, but I have multiple ranges that I need to apply it to, and I've been unsuccessful at adjusting it properly.
H17, H18, H20, H22 all need the same thing applied to it. Also if there is a way to make two conditions so if the cell value is -10 or less or 10 or more, that'd be very helpful as well.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Do all four cells need to meet the conditions, or only the cell that is changed?

What if more than one of the cells is changed at the same time and not all of the changed cells meet the conditions (as could happen, for example, with copy/pastespecial/add) ?
 
Upvote 0
Only the cell that is changed, but for all of those cells, the effect is the same (unhiding column I)

So if multiple cells were say copy & pasted, then as long as one of them triggered it, the column would unhide. Also, unhiding would take priority over hiding, so if multiple cells got entered all at once if any cell required I to be unhidden, then it would stay that way.
 
Upvote 0
H17, H18, H20, H22 all need the same thing applied to it.

You can use your test, just add the other cells, like:

Code:
If Not Intersect(Target, [H17,H18,H20,H22]) Is Nothing Then

The rest of your questions depend on your answer to footoo's questions.
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, cel As Range
Set rng = Intersect(Target, [H16:H18,H20,H22])
If Not rng Is Nothing Then
    For Each cel In rng
        If cel <= -10 Or cel >= 10 Then
            [I1].EntireColumn.Hidden = False
            Exit For
        Else: [I1].EntireColumn.Hidden = True
        End If
    Next
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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