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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
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,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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