Public variables

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
In general, is it preferable to pass variables instead of declaring a variable public (so that its value can be used in separate modules)?

If not, what's the alternative to declaring variables publically?

I've experimented with using classes but it seems an overkill (and even then I have to declare it public once), eg:

In a module, I have:

Code:
Global MyCol As ClsVariables

This is ClsVariables:

Code:
Public Property Get Col() As Integer
    Col = pCol
End Property
Public Property Let Col(ByVal Co As Integer)
    pCol = Co
End Property
 

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.
I suspect the most robust way is passing variables through, but for Excel's fairly 'lite' environment I'm happy just creating a separate module (not class) for variables and declaring them all in there, and using them as you see fit. Not saying that's the best, or 'correct' way, but it works for me.

/AJ
 
Upvote 0
It is generally better to limit the scope of variables to as narrow as possible, so passing variables between procedures is usually preferable. It is rarely necessary to have a Public variable but it can occasionally be simplest.
 
Upvote 0
It is generally better to limit the scope of variables to as narrow as possible, so passing variables between procedures is usually preferable. It is rarely necessary to have a Public variable but it can occasionally be simplest.

I've just converted one of my programs from using classes to passing variables and it works (and is easier to understand).

Can you think of an example where you HAVE (not just because it's easier) to declare a variable as public?
 
Upvote 0
Not offhand, no. I think there's probably always a way to avoid it. Having said that, I imagine there could be situations where a Public variable would be less confusing than the alternative; much like Goto - avoid as a rule, but be prepared to break the rule as long as you have a good reason, and know what you're doing.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

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