Worksheet Change help

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
I am trying to make certain objects hide automatically based on the number typed into a cell. I thought I was doing okay, but the second part of my code doesn't seem to be functioning.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address(False, False) = "A12" Then
        If Len(Target.Value) = 1 Then
            ActiveSheet.Shapes("G_1").Visible = True
            ActiveSheet.Shapes("G_2").Visible = False
            ActiveSheet.Shapes("G_3").Visible = False
            ActiveSheet.Shapes("G_4").Visible = False
        End If
    End If
    If Target.Address(False, False) = "A12" Then
        If Len(Target.Value) = 2 Then
            ActiveSheet.Shapes("G_1").Visible = False
            ActiveSheet.Shapes("G_2").Visible = True
            ActiveSheet.Shapes("G_3").Visible = False
            ActiveSheet.Shapes("G_4").Visible = False
        End If
    End If
End Sub

I'm hoping this is just something pretty simple that I am not doing correctly.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I should add that I am trying to hide/unhide shapes that all have the same name. I don't know if there is an alternative way to hide what I want.
 
Upvote 0
Perhaps

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) <> "A12" Then Exit Sub
Select Case Len(Target.Value)
    Case 1
        ActiveSheet.Shapes("G_1").Visible = True
        ActiveSheet.Shapes("G_2").Visible = False
        ActiveSheet.Shapes("G_3").Visible = False
        ActiveSheet.Shapes("G_4").Visible = False
    Case 2
        ActiveSheet.Shapes("G_1").Visible = False
        ActiveSheet.Shapes("G_2").Visible = True
        ActiveSheet.Shapes("G_3").Visible = False
        ActiveSheet.Shapes("G_4").Visible = False
End Select
End Sub
 
Upvote 0
I didn't seem to get anything to happen with that. Is it supposed to run the code for case 1 if the cell contents are '1'?
 
Upvote 0
It is based on your original - i.e. the number of characters. Entering 1 or 2 or A should trigger Case 1 and so on.
 
Upvote 0
I am still encountering the same issue with case 2. When case 1 runs G_2 gets hidden, but when I try to run case 2, it does not reappear.
 
Upvote 0
I tested VOG's code it works just fine.
you are currently using the "LEN" length function . this counts the number of characters in the cell.
if you want to be able to type values "1" or "2" which are both 1 character in length. you need to modify your code to this.
really just remove the LEN function.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) <> "A12" Then Exit Sub
Select Case Target.Value
    Case 1
        ActiveSheet.Shapes("G_1").Visible = True
        ActiveSheet.Shapes("G_2").Visible = False
        ActiveSheet.Shapes("G_3").Visible = False
        ActiveSheet.Shapes("G_4").Visible = False
    Case 2
        ActiveSheet.Shapes("G_1").Visible = False
        ActiveSheet.Shapes("G_2").Visible = True
        ActiveSheet.Shapes("G_3").Visible = False
        ActiveSheet.Shapes("G_4").Visible = False
End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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