Compile error: Constants, ...Declare statements not allowed

veritasca

Board Regular
Joined
May 19, 2004
Messages
50
Hi,

I am a newbie at Excel/VBA. This is my first posting.
I copied these functions from a book and placed in the General Declarations section as shown, but I got this compile error:

Compile error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules.

Here are the functions:

Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long

I am not sure how to fix this. I moved these Declarations inside the calling Sub, but then I got another compile error:
Only comments may appear after End Sub, End Function, or End Property

I am using Excel 2000 on Win2000 OS. If you have any ideas on what the problem might be, I would appreciate hearing them! Thanks.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi - Welcome to the board

Is that code at the very very top of the module above the first Sub?

It compiles file for me, just make sure it is above te very first sub in the module.
 
Upvote 0
Re: Compile error: Constants, ...Declare statements not allo

Thanks for the reply. I realized I didn't specify that I don't have a Module. My code is inside a worksheet. Does this make any difference?
When do you put code inside a Module versus inside a Worksheet?

I did put those statements at the very top of the Worksheet in the General Declarations section, above all the Subs. That's where I got the error. Is there anything else I can try?
 
Upvote 0
It has to go in a module, not a worksheet code section. Put the code in a standard module, then call it from the worksheet module (if you need to).
 
Upvote 0
Re: Compile error: Constants, ...Declare statements not allo

Beautiful! That was the key. Putting the declarations inside a Module and calling it from my Worksheet Sub. It worked! THANK YOU! :biggrin:
 
Upvote 0
Re: Compile error: Constants, ...Declare statements not allo

By the way, what is Lib "kernel32"? I tried looking it up in Help, but didn't find anything.
 
Upvote 0
Re: Compile error: Constants, ...Declare statements not allo

I think it is the Windows API.
 
Upvote 0
Re: Compile error: Constants, ...Declare statements not allo

Yes, you are right it is WIN32 API. Hmm! Where do I find out more about the specific functions available for Excel/VBA?
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,387
Members
449,445
Latest member
JJFabEngineering

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