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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,215,581
Messages
6,125,657
Members
449,247
Latest member
wingedshoes

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