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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
 Public qnty As Double

Public Price As Double
    [COLOR=#ff0000][B]Price = Workbooks("Book1.xls").Sheets("Sheet1").Range("E3").Valu[/B]e
[/COLOR]Public stdrdP As Double

Sub quote()...

...

I have the piece of code above various "subs" I have in the same module. Is this wrong?
The red line of code above is in wrong place.
You can't set the module variables between their declarations.

Use code like this:
Rich (BB code):
Public qnty As Double
Public Price As Double
Public stdrdP As Double
 
 
Sub Init()
 
  ' Trap error if references are not valid
  On Error GoTo exit_
 
  ' Change to suit
  qnty = Workbooks("Book2.xls").Sheets("Sheet2").Range("A1").Value
  Price = Workbooks("Book1.xls").Sheets("Sheet1").Range("E3").Value
  stdrdP = Workbooks("Book3.xls").Sheets("Sheet3").Range("B2").Value
 
exit_:
 
  If Err Then MsgBox Err.Description, vbExclamation, "Init error"
 
End Sub
 
Last edited:
Upvote 0
I have to admit that I have not worked a lot with Public Variables, especially ones that are not constant.
See if these two links provide any help/guidance:
VBA Variables and Constants
excel vba - How do I declare a global variable in VBA? - Stack Overflow


Thanks for all the help! I think from what I've seen I'm just gonna have to do a little extra coding to make everything work in each macro. Learned a bit more about variables though...

@ZVI Thanks for the tip!
 
Upvote 0

Forum statistics

Threads
1,216,217
Messages
6,129,570
Members
449,518
Latest member
srooney

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