Code runs but not from a button.

Russk68

Well-known Member
Joined
May 1, 2006
Messages
589
Office Version
  1. 365
Platform
  1. MacOS
On a Mac. This code runs fine from the editor but doesn't run from a shape with the macro assigned to it. I tried a few time just to make sure that I was assigning the correct macro.
Thanks in advance!
VBA Code:
Sub ToggleSheetVisibility()
    Dim shtName As Range
    Dim tabsUpdated As Boolean
    tabsUpdated = False
    For Each shtName In Range("L18:L42")
        On Error Resume Next ' <-- Ignore any sheets that are not found
        Dim sht As Worksheet
        Set sht = Worksheets(shtName.Value)
        If Not sht Is Nothing Then
            Dim col As Integer
            For col = 11 To 12 ' <-- Loop through columns K to L
                If Cells(shtName.Row, col).Value = True Then
                    sht.Visible = True ' <-- Unhides the sheet
                    tabsUpdated = True
                    Exit For
                End If
            Next col
            If col = 13 Then ' <-- None of the cells were true
                sht.Visible = False ' <-- Hides the sheet
                tabsUpdated = True
            End If
        End If
    Next shtName
    If tabsUpdated Then
        MsgBox "Tabs were updated"
    End If
End Sub
/CODE]
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Is your button on the sheet that contains Range("L18:L42") ?
The code is assuming that the List of sheet names is on the Active Sheet.
 
Upvote 0
Have you tried clicking New when assigning the macro to the shape and then pasting your code inside of the new macro?
 
Upvote 0
On a Mac. This code runs fine from the editor but doesn't run from a shape with the macro assigned to it. I tried a few time just to make sure that I was assigning the correct macro.
Thanks in advance!
VBA Code:
Sub ToggleSheetVisibility()
    Dim shtName As Range
    Dim tabsUpdated As Boolean
    tabsUpdated = False
    For Each shtName In Range("L18:L42")
        On Error Resume Next ' <-- Ignore any sheets that are not found
        Dim sht As Worksheet
        Set sht = Worksheets(shtName.Value)
        If Not sht Is Nothing Then
            Dim col As Integer
            For col = 11 To 12 ' <-- Loop through columns K to L
                If Cells(shtName.Row, col).Value = True Then
                    sht.Visible = True ' <-- Unhides the sheet
                    tabsUpdated = True
                    Exit For
                End If
            Next col
            If col = 13 Then ' <-- None of the cells were true
                sht.Visible = False ' <-- Hides the sheet
                tabsUpdated = True
            End If
        End If
    Next shtName
    If tabsUpdated Then
        MsgBox "Tabs were updated"
    End If
End Sub
/CODE]
VBA Code:
Sub Oval1_Click()

Call ToggleSheetVisibility

End Sub
Sub ToggleSheetVisibility()
    Dim shtName As Range
    Dim tabsUpdated As Boolean
    tabsUpdated = False
    For Each shtName In Range("L18:L42")
        On Error Resume Next ' <-- Ignore any sheets that are not found
        Dim sht As Worksheet
        Set sht = Worksheets(shtName.Value)
        If Not sht Is Nothing Then
            Dim col As Integer
            For col = 11 To 12 ' <-- Loop through columns K to L
                If Cells(shtName.Row, col).Value = True Then
                    sht.Visible = True ' <-- Unhides the sheet
                    tabsUpdated = True
                    Exit For
                End If
            Next col
            If col = 13 Then ' <-- None of the cells were true
                sht.Visible = False ' <-- Hides the sheet
                tabsUpdated = True
            End If
        End If
    Next shtName
    If tabsUpdated Then
        MsgBox "Tabs were updated"
    End If
End Sub
 
Upvote 0
Sorry, I assumed by your question you were using a "Shape". Are using a drawn shape or an inserted Command Button?
 
Upvote 0
Is your button on the sheet that contains Range("L18:L42") ?
The code is assuming that the List of sheet names is on the Active Sheet.
and the same question with the line below (for the same reason)
VBA Code:
If Cells(shtName.Row, col).Value = True Then
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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