Two Worksheet_SelectionChange on one worksheet

elmacay

Board Regular
Joined
May 4, 2006
Messages
88
Hi everyone,

I have a sheet with two blocks of data. On both blocks, I want to hide a different number of rows, using the following code. The thing is that VBA says I can't have two of the same. Is there a way to have one (or more) macro(s) have multiple Worksheet_SelectionChange(ByVal Target As Range), with each one looking at one of the two blocks? So I fill my block of data, change the number of rows I want to change in that block, and move on to the next block, etc. The alternative is to have a regular macro in a module and add it to a button, but I'm exploring this option first. Any help is greatly appreciated!

Cheers, Elmacay

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim w As Worksheet
    If Target.Address = "$N$52" Then
        If IsNumeric(Target.Value) Then
            If Target.Value > -1 And Target.Value < 17 Then
                Set w = ActiveWorkbook.Sheets("Invoerblad woninggegevens")
                w.Range("A54:A69").EntireRow.Hidden = False
                w.Range("A" & Target.Value + 54 & ":A69").EntireRow.Hidden = True
            End If
        End If
    End If
End Sub
________________________________________________________________________

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim w As Worksheet
    If Target.Address = "$N$102" Then
        If IsNumeric(Target.Value) Then
            If Target.Value > -1 And Target.Value < 17 Then
                Set w = ActiveWorkbook.Sheets("Invoerblad woninggegevens")
                w.Range("A104:A119").EntireRow.Hidden = False
                w.Range("A" & Target.Value + 104 & ":A119").EntireRow.Hidden = True
            End If
        End If
    End If
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim w As Worksheet
    If Target.Address = "$N$52" Then
        If IsNumeric(Target.Value) Then
            If Target.Value > -1 And Target.Value < 17 Then
                Set w = ActiveWorkbook.Sheets("Invoerblad woninggegevens")
                w.Range("A54:A69").EntireRow.Hidden = False
                w.Range("A" & Target.Value + 54 & ":A69").EntireRow.Hidden = True
            End If
        End If
    ElseIf Target.Address = "$N$102" Then
        If IsNumeric(Target.Value) Then
            If Target.Value > -1 And Target.Value < 17 Then
                Set w = ActiveWorkbook.Sheets("Invoerblad woninggegevens")
                w.Range("A104:A119").EntireRow.Hidden = False
                w.Range("A" & Target.Value + 104 & ":A119").EntireRow.Hidden = True
            End If
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,822
Members
449,096
Latest member
Erald

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