Vba formula not woring

caet_

New Member
Joined
Nov 22, 2020
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
Hello all!

I am trying to use this code on VBA. However, it is not working. Nothing happens when I change cell A1. Any idea why?

VBA Code:
Private Sub Worksheet_Change2(ByVal Target As Range)
If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
ActiveSheet.Unprotect Password:="123"
Application.ScreenUpdating = False
With Sheets("Sheet2")
Select Case Target.Address
Case "$A$1"
Select Case Target.Value
  Case Is = 1
        .Rows("107:323").EntireRow.Hidden = True
        .Rows("36:106").EntireRow.Hidden = False
   Case Is = 2
        .Rows("37:107").EntireRow.Hidden = True
        .Rows("108:177").EntireRow.Hidden = False
        .Rows("178:323").EntireRow.Hidden = True
   Case Is = 3
        .Rows("37:179").EntireRow.Hidden = True
        .Rows("180:250").EntireRow.Hidden = False
        .Rows("178:323").EntireRow.Hidden = False
Case Is = 4,5
        .Rows("37:252").EntireRow.Hidden = True
        .Rows("253:324").EntireRow.Hidden = False
      
    End Select
    End If
    ActiveSheet.Protect Password:="123"
    Application.ScreenUpdating = True
    End Sub

Thank you!
 
put this code below in sheet module
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a5")) Is Nothing Then

    Application.EnableEvents = False
    Application.Run "Macro1_" & Target
    Application.EnableEvents = True
End If
End Sub
and code below in regular code module
Code:
Sub Macro1_1()
With Sheets("Sheet2")
        .Rows("107:323").EntireRow.Hidden = True
        .Rows("36:106").EntireRow.Hidden = False
        End With
End Sub
Sub Macro1_2()
With Sheets("Sheet2")
        .Rows("37:107").EntireRow.Hidden = True
        .Rows("108:177").EntireRow.Hidden = False
        .Rows("178:323").EntireRow.Hidden = True
        End With
End Sub
Sub Macro1_3()
With Sheets("Sheet2")
       .Rows("37:179").EntireRow.Hidden = True
        .Rows("180:250").EntireRow.Hidden = False
        .Rows("178:323").EntireRow.Hidden = False
        End With
End Sub
Sub Macro1_4()
With Sheets("Sheet2")
        .Rows("37:252").EntireRow.Hidden = True
        .Rows("253:324").EntireRow.Hidden = False
        End With
End Sub
Sub Macro1_5()
With Sheets("Sheet2")
       .Rows("37:252").EntireRow.Hidden = True
        .Rows("253:324").EntireRow.Hidden = False
        End With
End Sub
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
put this code below in sheet module
VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a5")) Is Nothing Then

    Application.EnableEvents = False
    Application.Run "Macro1_" & Target
    Application.EnableEvents = True
End If
End Sub
and code below in regular code module
Code:
Sub Macro1_1()
With Sheets("Sheet2")
        .Rows("107:323").EntireRow.Hidden = True
        .Rows("36:106").EntireRow.Hidden = False
        End With
End Sub
Sub Macro1_2()
With Sheets("Sheet2")
        .Rows("37:107").EntireRow.Hidden = True
        .Rows("108:177").EntireRow.Hidden = False
        .Rows("178:323").EntireRow.Hidden = True
        End With
End Sub
Sub Macro1_3()
With Sheets("Sheet2")
       .Rows("37:179").EntireRow.Hidden = True
        .Rows("180:250").EntireRow.Hidden = False
        .Rows("178:323").EntireRow.Hidden = False
        End With
End Sub
Sub Macro1_4()
With Sheets("Sheet2")
        .Rows("37:252").EntireRow.Hidden = True
        .Rows("253:324").EntireRow.Hidden = False
        End With
End Sub
Sub Macro1_5()
With Sheets("Sheet2")
       .Rows("37:252").EntireRow.Hidden = True
        .Rows("253:324").EntireRow.Hidden = False
        End With
End Sub
Thank you. I have tried, but it does not work. :( However it does not display an error either.
 
Upvote 0
Rich (BB code):
did you put below code in sheet1 module?

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a5")) Is Nothing Then

Application.EnableEvents = False
Application.Run "Macro1_" & Target
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Rich (BB code):
did you put below code in sheet1 module?

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("a5")) Is Nothing Then

Application.EnableEvents = False
Application.Run "Macro1_" & Target
Application.EnableEvents = True
End If
End Sub
Yes yes, I've put in sheet 1 module.
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Intersect(Target, Range("A5")) Is Nothing Then Exit Sub
   Application.ScreenUpdating = False
   With Sheets("Sheet2")
      .Unprotect Password:="123"
      Select Case Target.Value
         Case Is = 1
            .Rows("107:323").EntireRow.Hidden = True
            .Rows("36:106").EntireRow.Hidden = False
         Case Is = 2
            .Rows("37:107").EntireRow.Hidden = True
            .Rows("108:177").EntireRow.Hidden = False
            .Rows("178:323").EntireRow.Hidden = True
         Case Is = 3
            .Rows("37:179").EntireRow.Hidden = True
            .Rows("180:250").EntireRow.Hidden = False
            .Rows("178:323").EntireRow.Hidden = False
         Case Is = 4, 5
            .Rows("37:252").EntireRow.Hidden = True
            .Rows("253:324").EntireRow.Hidden = False
      End Select
      .Protect Password:="123"
   End With
   Application.ScreenUpdating = True
End Sub
This needs to go in the Sheet1 code module.
 
Upvote 0
Solution
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Intersect(Target, Range("A5")) Is Nothing Then Exit Sub
   Application.ScreenUpdating = False
   With Sheets("Sheet2")
      .Unprotect Password:="123"
      Select Case Target.Value
         Case Is = 1
            .Rows("107:323").EntireRow.Hidden = True
            .Rows("36:106").EntireRow.Hidden = False
         Case Is = 2
            .Rows("37:107").EntireRow.Hidden = True
            .Rows("108:177").EntireRow.Hidden = False
            .Rows("178:323").EntireRow.Hidden = True
         Case Is = 3
            .Rows("37:179").EntireRow.Hidden = True
            .Rows("180:250").EntireRow.Hidden = False
            .Rows("178:323").EntireRow.Hidden = False
         Case Is = 4, 5
            .Rows("37:252").EntireRow.Hidden = True
            .Rows("253:324").EntireRow.Hidden = False
      End Select
      .Protect Password:="123"
   End With
   Application.ScreenUpdating = True
End Sub
This needs to go in the Sheet1 code module.
It worked! Thank you so much!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,757
Members
448,991
Latest member
Hanakoro

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