VBA to Hide/Unhide Shape Automatically When Cell Value Is Changed

Magdoulin

Board Regular
Joined
Jan 11, 2013
Messages
73
Hello Guys,


I'm trying to set event in my sheet that when the value of Cell "M1" in "Sheet Home" is changed to unhide specific shape.

I used the following event and code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("M1")) Is Nothing Then HideUnhideShape
End Sub


Sub HideUnhideShape()
If Worksheets("Home").Range("M1") = "" Then
ActiveSheet.Shapes("Rectangle 1").Visible = False
Else
ActiveSheet.Shapes("Rectangle 1").Visible = True
End If
End Sub

I've tried this as well:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("M1")) Is Nothing Then Call HideUnhideShape
End Sub


Sub HideUnhideShape()
If Worksheets("Home").Range("M1") = "" Then
ActiveSheet.Shapes("Rectangle 1").Visible = False
Else
ActiveSheet.Shapes("Rectangle 1").Visible = True
End If
End Sub

None of them worked although the code separately worked just fine, it seems that the issue is with the event

I'm saving the event in the "Home" sheet macro whereas the code in separated Module

I wish this forum allows to attach my file with the post to ease for you checking the issue, would you please help, thank you.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Are you trying to run a sheet change event script in one sheet and have it hide a shape in a different sheet?

I see your using sheet named "Home" and then your using Active sheet.

Is the active sheet named "Home"
 
Upvote 0
The shape is in Home sheet yes

anyways I changed the code in the Module to be like this but it worked separately perfectly but not with the event:

Code:
Sub HideUnhideShape()
If Worksheets("Home").Range("M1") = "" Then
Worksheets("Home").Shapes("Rectangle 1").Visible = False
Else
Worksheets("Home").Shapes("Rectangle 1").Visible = True
End If
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("M1")) Is Nothing Then
    
    If Target.Value = "" Then
    ActiveSheet.Shapes("Rectangle 1").Visible = False
Else
    ActiveSheet.Shapes("Rectangle 1").Visible = True
    End If
End If
End Sub
 
Upvote 0
It didn't work
I'm sorry

Note
It worked in new sheet
However, it didn't work with my particular project
It seems that there's something wrong with it
This of course requires to attach the file of my project
The forum doesn't allow this
I don't know how to continue then
 
Last edited:
Upvote 0
What is the name of the sheet where you are changing the value in range "M1" ?
And what is the name of the sheet with the shape you want to hide?
 
Upvote 0
Assuming now we are dealing with two different sheets try this:
Put this script in the sheet where you plan to change the value in range "M1"

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("M1")) Is Nothing Then
    
    If Target.Value = "" Then
    Sheets("Home").Shapes("Rectangle 1").Visible = False
Else
    Sheets("Home").Shapes("Rectangle 1").Visible = True
    End If
End If
End Sub
 
Upvote 0
No, both of them in the same sheet
I don't know what is wrong especially that your event worked just fine in new file
 
Upvote 0
Well if we are only dealing with one sheet then my script should have worked.

When you say it did not work you need to explain what it did.

Are you changing the value in M1 manually of is the change being done by a formula causing the change?
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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