Public variable question

MrKremrik

Board Regular
Joined
Jul 16, 2012
Messages
56
Hi all,

I have several macros in a personal folder that all need to reference the same variables located in a single file. So I declared these variables as public. But I don't know the syntax for assigning a cell value to these. Something like: Public Price as Double. And then: Price = workbook.xls-sheet1-"E3". I know that's not right, but hopefully it gets the point across. Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I have never tried this before, but it seems like it should work, in theory.
Since the "Personal Macro Workbook" is actually a workbook itself, why not store your variables/values there?
They should then always be open and available, just like the Personal Macros Workbook macros are.
 
Upvote 0
Something like...

Price = Workbooks("Book1.xls").Sheets("Sheet1").Range("E3").Value

Okay, that didn't work. It said "Invalid Outside Procedure".

@Joe4. Unless I don't fully understand what you're recommending, I don't know that it would work. The file needs user input in E3 of the aforementioned file (which is not the personal.xls workbook). The value in this cell needs to be a variable that 5 different macros can use, just so I can eliminate a ton of superfluous coding.

Thanks!
 
Upvote 0
Sorry, I thought you might be working with "static" variables, and not a user input ones that change.
 
Upvote 0
Is the workbook you need to reference open when you are trying to use the formula?
 
Upvote 0
AlphaFrog's syntax seems to work fine for me.

Could you post your code, where you are trying to use it?
 
Upvote 0
Code:
 Public qnty As Double
Public Price As Double
    Price = Workbooks("Book1.xls").Sheets("Sheet1").Range("E3").Value
Public stdrdP As Double

Sub quote()...

...

I have the piece of code above various "subs" I have in the same module. Is this wrong?
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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