VBA Sub runs from Developer Ribbon and macro book but not when assigned to a button

Dispa

New Member
Joined
Oct 5, 2011
Messages
13
Hello there!

I've been using VBA for a few months and so far it's been a very fulfilling experience. Just this one time i've bumped into a weird problem and were not able to find a solution.

It goes like this. I am designing a "Settings" tab on a fairly large report. It has data sheets, proccessing sheets and exhibition sheets. Just the latter are user friendly, and the others are meant to be accessed by an administrator only. Since the administrator is not VBA trained as well, my efforts right now are directed at making the settings administrator-friendly. My data tabs and proccessing tabs all work underneath the report being "Very Hidden". So to allow for sporadic updates, i've built a macro that unhides these sheets, whose names are stored in a named range called "hide".

Here's what my code looked like:

Code:
[COLOR=#373E4D][FONT=helvetica]Sub Exhibit()

Dim ws As Range
Dim st As String

For Each ws In Range("Hide")
st = Cells(ws.Row, ws.Column).Value
Worksheets(st).Visible = True
Next ws

End Sub[/FONT][/COLOR]

I've found that it works when i run it from the Macros button on the Developer Ribbon, and also directly from my Visual Basic Macro Book. But when i assign it to a form (my "button" of sorts), it returns a Runtime Error # 9.

In order to troubleshoot i've separated the data and proccessing sheets and found that the error is the same for both (using " ' " to make lines turn to comments on the code):

Code:
Sub Exhibit()

Dim ws As Range
Dim st As String


For Each ws In ActiveSheet.Range("Hidedata")
On Error Resume Next
st = Cells(ws.Row, ws.Column).Value
Sheets(st).Visible = True
Next ws


'For Each ws In ActiveSheet.Range("Hideprocs")
'On Error Resume Next
'st = Cells(ws.Row, ws.Column).Value
'Sheets(st).Visible = True
'Next ws


End Sub

Running the code with each one of the two parts activated at wonce yielded the same result: Just two of the four sheets in each case were made visible, and i did not get an error because of the "On Error Resume Next".
I guess i can live without this code working, but I would very much have some light shed on why could it work from the Macro Book and not from the Button. Any help will be very appreciated! Thanks for the attention to anyone who has read this far!

Using Excel 2013 on Windows 10.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I wonder if it the code doesn't understand what wb or worksheet you are referring to.

Code:
Sub Exhibit()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim rng As Range

    Set wb = ThisWorkbook
    Set ws = Sheets("MySheet")    'sheet with the range 'Hide'

    With wb
        With ws
            For Each rng In .Range("Hide")
                Sheets(rng.Value).Visible = False
            Next rng
        End With
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,764
Messages
6,126,751
Members
449,336
Latest member
p17tootie

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