In VBA, I generally define shared variables as Public as opposed to building references via byVal or byRef. Most commonly, I call a subroutine or function to establish a value for said variable. Will someone please explain to me the benefit of using byVal/byRef vs simply creating a public var?
Related Questions in EXCEL
- Power Query / M Code, extract a list of tables into one main table, some column headers same but some different and in different order (and in row 2)
- Is there a way to validate the cell format (from excel) to fetch the symbol from it (in Java)?
- Excel - Visual Basic, macro with autofill "1"
- Getting Run-time error '13': Type Mismatch using .Find
- Getting website metadata (Excel VBA/Python)
- Excel Code Editor doesn't work (blank window)
- How to find out how many of each 2, 3 and 4 required to fit in 100 using excel?
- How would I apply a rather complex summation formula like this in Excel?
- Removing a Button from Customized Excel Ribbon
- Excel - Update Item Description Based on Accessories Ordered with It
- select duplicates from data based on another column
- How to use VBA to bold just some text
- VBA Code to filter and get values from csv to excel worksheet
- Look up max alpha numeric value
- Azure Batch for Excel VBA
Related Questions in VBA
- Toggle "conversation view" in Outlook with VBA
- VBA query - sort text in alphabetical order in Word
- Excel - Visual Basic, macro with autofill "1"
- Getting Run-time error '13': Type Mismatch using .Find
- Getting website metadata (Excel VBA/Python)
- How to use VBA to bold just some text
- VBA Code to filter and get values from csv to excel worksheet
- Azure Batch for Excel VBA
- How can i printpreview multiple excel sheets, with the names of the sheets located in a range?
- Comparison the data of two row and unique number highlight and show below of it
- Is there a way to pass Today's date as a command line argument to Excel from a Windows Task Scheduler Job
- Xero Upload Invoice file using API
- Visual Basic For Application Related Question
- Trying to give color to column field headers in pivot table with vba
- General error handler: If any error in UserForm other than msgBox: Err.Description
Related Questions in PUBLIC
- How to do DSA verification using java.security library
- Laravel app inside Wordpress website run on app folder and not in app/public folder?
- Access image folder from Laravel Project A to Laravel B, C, D etc
- S3 does not return any errors but does not upload the file to the cloud
- java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
- Override public static
- Styles, font and js don't load on laravel deployed on shared hosting
- How can I get public IP address in python programetically. I don't want to hit any 3rd party service url to get the IP
- Laravel Image link redirecting to login page in localhost
- how to return a std::expect<T, E> from a function where T's move/copy constructor are deleted and has a private constructor
- Question about Friend functions used in C++
- change public directory to public_html in laravel 10 and fixing vite manifest not found error
- Separating git commits for private and public
- Button Audio stopper not stopping the Button
- How to publish SharePoint calendar in website
Related Questions in BYREF
- Cannot express a ref-like struct with other ref-like struct members [<IsByRefLike; Struct>]
- Hi ... Ever used byval and byref in arrays in VB??... I passed an array byval then byref... in both, ARRAY changes are brought out of procedure..why?
- How to upcast `byref<'t>` to `byref<obj>` in F#?
- [F#][sharppcap][Error] "A type instantiation involves a byref type." what is a workaround in F#
- VBA compile error due to "ByRef argument type mismatch"
- byval vs byref when passing object
- How to get VBAs ByRef return from Python over COM
- Why VBA Class cannot pass ByRef Array()?
- Passing Dictionary ByRef Into a Private Sub in excel VBA userform module gives ByRef type mismatch error
- ByVal/ByRef to copy data [VBA]
- initial array in size by ref c++
- VBA: Compile Error ByRef Argument Type Mismatch in Private Sub
- How to start a Backgroundworker ByRef
- python 3.9 x64 ctypes pass char pointer by reference (char**) OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF
- trying to pass by reference in F#
Related Questions in BYVAL
- Hi ... Ever used byval and byref in arrays in VB??... I passed an array byval then byref... in both, ARRAY changes are brought out of procedure..why?
- byval vs byref when passing object
- ByVal/ByRef to copy data [VBA]
- How do I use Change(byVal Target as Range) to check/change cell color
- Store new line of data when values change in another cell(s) - change value macro
- Sas how to have 2 byvar in a display
- Cannot delete the cells with old data in them to make way for new data
- Getting 1004 error on code that worked after adding new code
- How do I execute instructions only if the cell value change?
- Does long support decimals?
- 2nd change(ByVal target As Range) doesnt work
- How do you update the same cell you've entered a value in, based on another cell?
- Looping for the macro
- VBA - Overflow error if passing certain digits to another sub
- VB.NET array parameter mechanism, byval and byref
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
This is something related to how you define the architecture. Well, VBA is not truly OOP language and thats the reason this question popped in your mind.
Public variabes hold (retain their value) till the time the instance of the module (they are declared in) is in memory. As for example, public variables is general Modules are hold till the ThisWorkbook is open (except if there is any runtime error). On the other hands, public variables in UserForm modules are hold till the instance of the UserForm is open. Public variables in Class Modules are "properties" and are hold till an instance variable of the class in in memory. In other OOP languages where everything is class based, there is no so called public variables ... they are, as I said "properties" and are hold till the class instance is loaded.
ByVal(byValue) or ByRef(byReference) are just a means of passing the variables as parameters (depending on what you need in the function).
Which is better : From my previous experience, Public variables in VBA are truly easy to implement but easier to mess with. Too many public variables = Too much time to debug. Use 2~3 public variables only and design your codes in classes to hold properties. This way when you jump to other languages you will be at home :)