byVal vs Public

mmaNate

New Member
Joined
Sep 15, 2011
Messages
3
I generally define shared variables as Public as opposed to passing them via byVal or byRef. Most commonly, I call a subroutine or function to establish a value for said variable, making it usable anywhere in my script. Will someone please explain to me the benefit of using byVal/byRef vs simply creating a Public var?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The notions are kind of perpendicular. Passing ByVal gives the called routine a copy of the variable (a copy of the pointer for object variables), which prevents the called procedure from changing them as they can with variables passed ByRef.

Making a variable Public just increases its visibility to all procedures in a project that don't have a variable of the same name declared more locally.

Public variables have their place, but they are generally less used than abused, and make code less atomic / reusable, and more difficult to debug.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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