Combine two Private Sub Worksheet_Change(ByVal Target As Range)

nealtd

New Member
Joined
May 20, 2016
Messages
19
Hi,

I have two events I am trying to combine on one sheet that I am having difficulty with. Both are where you select a number in one cell and it unhides that many rows. When I combine them, only the top one works. I have tried switching them and still the same results.


Private Sub Worksheet_Change(ByVal Target As Range)
Dim r1 As Range, r2 As Range, n1 As Long, n2 As Long
Set r1 = Me.Range("C9")
Set r2 = Me.Range("C33")

If Not Intersect(Target, r1) Is Nothing Then
With Me.Range("A10:A29")
.EntireRow.Hidden = True
n1 = r1.Value
If n1 > 0 Then
.Cells(1, 1).Resize(n1, 1).EntireRow.Hidden = False
End If

If Not Intersect(Target, r2) Is Nothing Then
With Me.Range("A34:A53")
.EntireRow.Hidden = True
n2 = r2.Value
If n2 > 0 Then
.Cells(1, 1).Resize(n2, 1).EntireRow.Hidden = False
End If

End With
End If
End With
End If

End Sub
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
It seems your End With's and End If's are a bit messed up... try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r1 As Range, r2 As Range, n1 As Long, n2 As Long
Set r1 = Range("C9")
Set r2 = Range("C33")

If Not Intersect(Target, r1) Is Nothing Then
    With Range("A10:A29")
        .EntireRow.Hidden = True
        n1 = r1.Value
        If n1 > 0 Then
            .Cells(1, 1).Resize(n1, 1).EntireRow.Hidden = False
        End If
    End With
End If

If Not Intersect(Target, r2) Is Nothing Then
    With Range("A34:A53")
        .EntireRow.Hidden = True
        n2 = r2.Value
        If n2 > 0 Then
            .Cells(1, 1).Resize(n2, 1).EntireRow.Hidden = False
        End If
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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