Private sub doesn't work

opislak

Board Regular
Joined
Feb 28, 2017
Messages
64
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
 

opislak

Board Regular
Joined
Feb 28, 2017
Messages
64
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.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,337
Office Version
  1. 2010
Platform
  1. Windows
i blame the wine when i have a brain fade 🍷
 

Forum statistics

Threads
1,147,498
Messages
5,741,505
Members
423,663
Latest member
kaveh87rsh

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
Top