Private sub doesn't work

opislak

Board Regular
Joined
Feb 28, 2017
Messages
68
Hi all,

I would like my macros to be private, so they would not be visible when a user tries out Alt-F8. That works for all my macros, except for those that are called upon by a button. Is that normal?
If I make the macro private, it returns this error when the button is pressed: "Compile error: Sub or Function not defined".
Here are the relevant parts of my two macros:
- in the sheet where the button is:
VBA Code:
Private Sub CommandButton2_Click()
    ShowSheets
End Sub

- in the module 1 where all my macro's are, it calls this macro to show all the sheets in the workbook. And that macro cannot be made "Private". Is that normal?

VBA Code:
Sub ShowSheets() 'Cannot be a Private Sub !!

        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        Application.Calculation = xlCalculationManual
       
        ActiveWorkbook.Unprotect "mypassword" 'yes, I changed my password :-)
        For Each ws In ThisWorkbook.Worksheets
            With ws
                .Unprotect "mypassword"
                .Visible = True
            End With
        Next ws
       
        Application.WindowState = xlMaximized
        Application.Calculation = xlCalculationAutomatic
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True

End Sub
 
probably my misunderstanding.
so why not grab the code from the Sub

VBA Code:
Private Sub CommandButton2_Click()
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
      
        ActiveWorkbook.Unprotect "mypassword" 'yes, I changed my password :-)
        For Each ws In ThisWorkbook.Worksheets
            With ws
                .Unprotect "mypassword"
                .Visible = True
            End With
        Next ws
      
        .WindowState = xlMaximized
        .Calculation = xlCalculationAutomatic
        .DisplayAlerts = True
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
Ooooowwwkaayyyyyy, now I really look stupid...
(please don't say "You are". Some things don't need to be confirmed)
Thank you diddi, you really helped me out.
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
i blame the wine when i have a brain fade ?
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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