Show/Hide rectangles based on cell value

lol.xls

Board Regular
Joined
Oct 5, 2009
Messages
174
Here's my scenario: I have a combobox with 4 values and 4 rectangles named after the combobox values. When the value is selected from the combobox, i want the appropriate rectangle to become visible. If the value in the combobox changes, I need the visibility of the rectangles to change accordingly as well. Here is what I have so far...any thoughts on why my rectangles aren't changing?

Below is within a Module:
Code:
Sub YourMacro()
    Dim myshape As Shape
    Dim Sshape As Shape
    Dim sh As Worksheet


    Application.ScreenUpdating = False


    For Each sh In ThisWorkbook.Worksheets
        If sh.Name <> "Sheet4" Then
            sh.Select
            For Each myshape In sh.Shapes
                If myshape.Type = msoShapeRectangle Then
                    myshape.Visible = False
                End If
            Next myshape
            
            On Error Resume Next
            Set Sshape = Nothing
            Set Sshape = sh.Shapes((Sheets("Sheet4").Range("Q7").Value))
            On Error GoTo 0


            If Not Sshape Is Nothing Then
                With sh.Shapes(Sheets("Sheet4").Range("Q7").Value)
                    .Visible = True
                End With


            End If
        End If
    Next sh
    Application.ScreenUpdating = True
End Sub

This Code is contained within the sheet to call the aforementioned code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range("Q7"), Target) Is Nothing Then
        If Target.Value <> "" Then
            Call YourMacro
        End If
    End If
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,

Are you sure that when you change the value of the combobox that you are actually triggering the code. You could make this change that will give you a msgbox to be sure...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range("Q7"), Target) Is Nothing Then
        If Target.Value <> "" Then
            MsgBox "running"
            Call YourMacro
        End If
    End If
End Sub

igold
 
Upvote 0
Yeah...I'm dumb. The code is fine. I had renamed Sheet4, and assumed that since it was still technically Sheet4, it would work. When I renamed the sheet everything executed just fine.
 
Upvote 0
I am glad you got it squared away.
 
Upvote 0

Forum statistics

Threads
1,215,540
Messages
6,125,409
Members
449,223
Latest member
Narrian

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