I have written a code to auto hide/unhide rows however code is not working

siddo

Board Regular
Joined
May 26, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
can you help me figure out - something is wrong with the code and I'm unable to figure out. The code does not auto hide/unhide the rows
VBA Code:
Private Sub Worksheet_Changes(ByVal Target1 As Range)

Application.ScreenUpdating = False
Application.DisplayAlerts = False


If Target1.Address = "$B$12" Then
    Dim b12val As String
    
Sheets("RawData").Rows.Hidden = False
    
b12val = Target1.Value
    
If b12val = 0 Then
        Sheets("RawData").Range("A13:B27").EntireRow.Hidden = True
   
ElseIf b12val = 1 Then
        Sheets("RawData").Range("A16:B27").EntireRow.Hidden = True
        
ElseIf b12val = 2 Then
        Sheets("RawData").Range("A19:B27").EntireRow.Hidden = True
        
ElseIf b12val = 3 Then
        Sheets("RawData").Range("A22:B27").EntireRow.Hidden = True
        
ElseIf b12val = 4 Then
        Sheets("RawData").Range("A25:B27").EntireRow.Hidden = True
        
ElseIf b12val = 5 Then
     Sheets("RawData").Rows.Hidden = False
    
End If
End If

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
PS : I already have one even code in the same sheet, is it possible to have 2 event code in the same sheet? if no, is there any workaround?
 
Upvote 0
The following works for me:

VBA Code:
Private Sub Worksheet_Change(ByVal Target1 As Range)
'
    If Target1.Address = "$B$12" Then
        Dim b12val As String
'
        Sheets("RawData").Rows.Hidden = False
'
        b12val = Target1.Value
'
        Select Case b12val
            Case Is = 0: Sheets("RawData").Range("A13:B27").EntireRow.Hidden = True
            Case Is = 1: Sheets("RawData").Range("A16:B27").EntireRow.Hidden = True
            Case Is = 2: Sheets("RawData").Range("A19:B27").EntireRow.Hidden = True
            Case Is = 3: Sheets("RawData").Range("A22:B27").EntireRow.Hidden = True
            Case Is = 4: Sheets("RawData").Range("A25:B27").EntireRow.Hidden = True
            Case Is = 5: Sheets("RawData").Rows.Hidden = False
        End Select
    End If
End Sub
 
Upvote 0
Solution
PS : I already have one even code in the same sheet, is it possible to have 2 event code in the same sheet? if no, is there any workaround?
You can have more than one event code in the same sheet, but not with the same name. If you have more than one that require the same name then you have to combine the codes into one sub routine.
 
Upvote 0
Alternative code:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With Sheets("Raw Data")
    .Rows.Hidden = False
    If Intersect(Target, .Range("B12")) Is Nothing Or Target.Value >= 5 Then Exit Sub
    .Range(.Cells(13 + Target.Value * 3, "A"), .Cells(27, "B")).EntireRow.Hidden = True
End With
End Sub
 
Upvote 0
You really don't have any friends do you? :p

Just kidding, Nice solution! Other than the sheet name change. :rolleyes:
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,823
Members
449,049
Latest member
cybersurfer5000

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