Sub vs Public Sub

User avatar
stuck
Panoramic Lounger
Posts: 8160
Joined: 25 Jan 2010, 09:09
Location: retirement

Sub vs Public Sub

Post by stuck »

My brain is fogged today, I should (and probably do) know this already...

If a Sub is not explicitly named as either Private or Public, it defaults to Public, right? i.e.:

Code: Select all

Sub myMacro ()
is the same as:

Code: Select all

Public Sub myMacro()
Thanks you for indulging my mental block.

Ken

User avatar
Leif
Administrator
Posts: 7208
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Sub vs Public Sub

Post by Leif »

I believe this is correct. See Sub statement (VBA) | Microsoft Docs:
If not explicitly specified by using Public, Private, or Friend, Sub procedures are public by default.
Leif

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sub vs Public Sub

Post by HansV »

What Leif says. A sub or function is public by default, while a variable or constant is private by default.
Best wishes,
Hans

User avatar
stuck
Panoramic Lounger
Posts: 8160
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: Sub vs Public Sub

Post by stuck »

Thank you Leif, that was the link I should have been able to find for myself.

Thank you too Hans for confirming how it's the other way round for variables.

Ken

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sub vs Public Sub

Post by HansV »

Best wishes,
Hans