Previous actions of target.Address remain

totesbotes

New Member
Joined
Jan 29, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am trying to do a user input whereby there are 2 inputs cell A1 and cell B1 on the same worksheet. For actions of cell A1, my code is as follows:

VBA Code:
If target.Address = "$A$1" Then
        Select Case Sheets("Input").Range("A1")
            Case "Hide1"
                Range("A3:A5").EntireRow.Hidden = True
            Case "Hide2"
                Range("A7:A9").EntireRow.Hidden = True
     End Select
End If

For actions of cell A2,my code is as follows:

VBA Code:
If target.Address = "$A$2" Then
        Select Case Sheets("Input").Range("A2")
            Case "Hide3"
                Range("A11:A13").EntireRow.Hidden = True
            Case "Hide4"
                Range("A15:A17").EntireRow.Hidden = True
     End Select
End If

Currently, I face the problem that when I change A1 to Hide1, Row 3-5 will hide but unhide immediately when I change A2 to Hide 3 while Row 11-13 gets hidden only.

How do I make sure that after changing A1, Row 3-5 continue to be hidden even after I change A2? E.g. I change A1 to "Hide 1", Rows 3-5 gets hidden. Next, I change A2 to "Hide 3", I want Rows 11-13 gets hidden while Rows 3-5 continues to get hidden.
 
Last edited by a moderator:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
A simple if usually works for me, in your case.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    With Sheets("Input")
        If Not Intersect(Target, Range("A1")) Is Nothing Then
            If Target = "Hide 1" Then
               .Range("3:5").EntireRow.Hidden = True
            ElseIf Target = "Hide 2" Then
               .Range("7:9").EntireRow.Hidden = True
            End If
        ElseIf Not Intersect(Target, Range("A2")) Is Nothing Then
            If Target = "Hide 3" Then
               .Range("11:13").EntireRow.Hidden = True
            ElseIf Target = "Hide 4" Then
               .Range("15:17").EntireRow.Hidden = True
            End If
        End If
    End With
    
End Sub
 
Upvote 0
Rather than all that branching, perhaps
VBA Code:
With Sheets("Input")
    .Range("3:5").EntireRow.Hidden = (.Range("A1") = "Hide 1")
    .Range("7:9").EntireRow.Hidden = (.Range("A1") = "Hide 2")
    .Range("11:13").EntireRow.Hidden = (.Range("A2") = "Hide 3")
    .Range("15:17").EntireRow.Hidden = (.Range("A4") = "Hide 4")
End With
 
Upvote 0
This code is working ok. I did not find any problem.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then
        Select Case Sheets("Input").Range("A1")
            Case "Hide1"
                Range("A3:A5").EntireRow.Hidden = True
            Case "Hide2"
                Range("A7:A9").EntireRow.Hidden = True
     End Select
End If

If Target.Address = "$A$2" Then
        Select Case Sheets("Input").Range("A2")
            Case "Hide3"
                Range("A11:A13").EntireRow.Hidden = True
            Case "Hide4"
                Range("A15:A17").EntireRow.Hidden = True
     End Select
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,941
Members
448,534
Latest member
benefuexx

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