Code for button in Visio

In Visio I like to use buttons to turn layers on and off. Here’s code that uses a single button for turning on and off the layer named ‘Switch’.

Private Sub CommandButton1_Click()

Dim LayersObj As Visio.Layers
Dim LayerObj As Visio.Layer
Dim LayerName As String
Dim LayerCellObj As Visio.Cell
Set LayersObj = ActivePage.Layers
For Each LayerObj In LayersObj
LayerName = LayerObj.Name
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
If LayerName = "Switch" Then
' Debug.Print LayerName
If LayerCellObj.Formula = False Then

LayerCellObj.Formula = True

Else
LayerCellObj.Formula = False
End If
End If
Next

End Sub