Compile Error: Invalid Outside Procedure

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
Excel prompt out an error message that : "Compile Error: Invalid Outside Procedure".

The code it highlights is "PR_Form" in code:
Code:
'-------------------------------------
'declaration of some PR Form variables
'-------------------------------------
Public Pr As Worksheet 'PR Form worksheet shorhand
Pr = ThisWorkbook.Worksheets("PR_Form") 'set value to Pr

I put this code outside subs.
How can I solve this problem?
Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi

You can't put executable code outside of procedures. You can declare variables and external functions (e.g. Windows API) but everything else must reside in some sort of procedure e.g. function, sub, property get/let/set.

Also, you need to use the Set keyword when assigning an object to an object variable i.e.

This:
Set Pr = ThisWorkbook.Worksheets("PR_Form") 'set value to Pr

not this:
Pr = ThisWorkbook.Worksheets("PR_Form") 'set value to Pr

HTH
DK
 
Upvote 0
Hi there,

Try this:

Code:
Set Pr = ThisWorkbook.Worksheets("PR_Form") 'set value to Pr

HTH

Robert
 
Upvote 0
Thanks DK and Robert,

I tried both of your solutions. I think DK's correct. Thank you so much!
Thanks a lot for DK's explanation too;)

Yours sincerely,
lolo^-^
Hi

You can't put executable code outside of procedures. You can declare variables and external functions (e.g. Windows API) but everything else must reside in some sort of procedure e.g. function, sub, property get/let/set.

Also, you need to use the Set keyword when assigning an object to an object variable i.e.

This:
Set Pr = ThisWorkbook.Worksheets("PR_Form") 'set value to Pr

not this:
Pr = ThisWorkbook.Worksheets("PR_Form") 'set value to Pr

HTH
DK
 
Upvote 0

Forum statistics

Threads
1,223,311
Messages
6,171,346
Members
452,397
Latest member
ddneptune

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