VBA Compile Error: Private object modules cannot be used in public object modules as parameters or return types for public procedures....

starl

Administrator
Joined
Aug 16, 2002
Messages
6,081
Office Version
  1. 365
Platform
  1. Windows
I've been rewriting a workbook written by someone else many years ago. I have never run into this error before and can't figure out what it's talking about:

In the ThisWorkbook module, I have a Workbook_Open event. Within it, it calls function InitMe.
If the function is Public, I get the error in the title when I compile
If I make the function Private, the error goes away.

I don't program within the book/sheet modules that much, except events. I've occasionally had events call other subs/functions, often stored in standard modules, not within the same special module. So I thought maybe this was a rule I had not run into (after xx years of programming, but you never know!) So, I created another workbook with an Open event and a call to a public function in the same module. No error.

Any idea what's going on? It's not a problem leaving it Private, but I'm just wondering why this workbook is acting this way.

Update: there's another function call in the Open and that function is Public without a problem...
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I can only think that there is an issue with how you're calling it or using the return value, or perhaps a version thing but we're both using 365?
3 messages upon workbook open with this code
Public Sub Workbook_Open()
TestPrivate 'testprivate is in a standard module
testprivate2
testprivate3
End Sub

Private Sub testprivate2()
MsgBox "test2"
End Sub

Public Sub testprivate3()
MsgBox "test3"
End Sub
 
Upvote 0
I guess you have a Class module somwhere in your project and the InitMe function has 1 or more params of that Class Type or is supposed to return an instance of that Class... If that's the case, then you should place the InitMe function in a Standard module or, if you still want to keep the InitMe function inside the ThisWorkbook module, you should change the Instancing property of the Class from the default Private to PublicNotCreatable,. Only then the Public InitMe function will compile.


Untitfffffffffffffffffled.png



This instancing stuff is more relevant when using classes between different vbaprojects.

Edit:
If the culprit is a paramenter in the initMe function, you can declare the parameter as Object and it should work w/o changing the class instancing attribute.
 
Last edited:
Upvote 0
Solution
Thank you both for getting back to me.

@Jaafar Tribak - you got it figured out! Now that you've spelled it out, the error message makes sense. Yes, InitMe has a parameter to a Class module InitMe (byval cProp as clsProperties)
thanks again
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

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