Home > OS >  Excel And Statement to Display Shape
Excel And Statement to Display Shape

Time:12-15

might be a simple ask but I am new around here & stumped!

If a cell contains a value, one shape is to show & one is to disappear, but I can't get the following code to do anything with the second shape. See below:

Sub Worksheet_Calculate()
     If Range("M17").Value = "Option A" Then
          Me.Shapes("Logo1").Visible = True And Me.Shapes("Logo3").Visible = False
          Else
           If Range("M17").Value = "Option B" Then
          Me.Shapes("Logo3").Visible = True And Me.Shapes("Logo1").Visible = False
            Else
            Me.Shapes("Logo3").Visible = False And Me.Shapes("Logo1").Visible = False
     End If
End If
End Sub

CodePudding user response:

As stated in the comments by Warcupine, And is a boolean operator and usually used in conditional statements (eg in If or While)

What you want to have is two different statements:

 Me.Shapes("Logo1").Visible = True 
 Me.Shapes("Logo3").Visible = False

If you insinst, you can put them on the same line by using : to separate both statements, but I would strongly advice against it:

Me.Shapes("Logo1").Visible = True : Me.Shapes("Logo3").Visible = False

You can, however, make your code easier:

Dim option as String 
option = Range("M17").Value 
Me.Shapes("Logo1").Visible = (option = "Option A")
Me.Shapes("Logo3").Visible = (option = "Option B")

The term (option = "Option A") resolves in either True or False and you can assign the result directly to the Visible-property

  • Related