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!
 

AC PORTA VIA

Board Regular
Joined
Apr 9, 2016
Messages
89
Office Version
  1. 365
Platform
  1. Windows
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
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

caet_

New Member
Joined
Nov 22, 2020
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
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.
 

AC PORTA VIA

Board Regular
Joined
Apr 9, 2016
Messages
89
Office Version
  1. 365
Platform
  1. Windows
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
 

caet_

New Member
Joined
Nov 22, 2020
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
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.
 

AC PORTA VIA

Board Regular
Joined
Apr 9, 2016
Messages
89
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

can you uploaded your file with all codes that you added from me
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,749
Office Version
  1. 365
Platform
  1. Windows
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.
 
Solution

caet_

New Member
Joined
Nov 22, 2020
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,749
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,072
Messages
5,628,476
Members
416,320
Latest member
Insightful111

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
Top