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?
vbval vs public variable
663 Views Asked by Nathan Green At
1
There are 1 best solutions below
Related Questions in EXCEL
- Concatenate excel cell string within cell reference string
- Use hidden information for filtering data
- Using Vlookup in Excel sheet to match substring
- Import from api into multiple excel cells
- Loop through list of files and open them
- Pull and push data from and into sql databases using Excel VBA without pasting the data in Excel sheets
- Loop with equation for upper limit
- excel vba null value in array
- Why is my xml file having these after convert from excel?
- TextToColumns function uses wrong delimiter
- Difference between two dates in excel 2013
- Concatenate string and number as number
- Why in a pivot the "include new items in manual filter" option is grey out when source is a powerpivot?
- Count Unique Values Repeated Dates
- How do I extract info from crunchbase
Related Questions in VBA
- Import from api into multiple excel cells
- Loop through list of files and open them
- Pull and push data from and into sql databases using Excel VBA without pasting the data in Excel sheets
- Loop with equation for upper limit
- excel vba null value in array
- TextToColumns function uses wrong delimiter
- Access Global not working
- Concatenate string and number as number
- How do I extract info from crunchbase
- Excel needs to pull substring values to variables
- Act on Outlook mail item when opened for editing
- Macro is not working
- Excel 2013 VBA opening multiple windows for a single workbook
- Hyperlink directs to hidden row
- How to modify data in a range of a column of cells that are not blank
Related Questions in PUBLIC
- Needing a private and public method for the same recursive function
- can es6 class have public properties as well as functions?
- Have Laravel load an alternate public folder on development machine
- returning reference to private vs public member
- Why do we usually start programs with public static void main('String[] args')?
- How to restrict public access to a webapplication?
- what is the range of public ip address ipv4?
- In Java, memory-wise does it make a difference to use private vs public?
- Java static reference vs dynamic reference and calls
- C#: Unable to access Public method in a private method
- Initialize a public variable
- How to hide the public API of dependencies (.jar libraries) in Android module?
- Public field from String - How can you get a public field vis its name(string)
- Could you tell me diffrence sub and public sub?
- How can I run this class in MainPage.cs?
Related Questions in BYREF
- ByRef Argument type mismatch on just changing the variable position in declaration
- How do I pass a variable from one function to another function?
- Pass array created in first function to second function
- Declare variables and pass them to one of two functions, or declare variables in each function? (possibly VB.NET specific)
- VBA ByRef argument type mismatch
- 'ByRef' parameter '<parametername>' cannot be used in a lambda expression
- Understanding byref, ref and &
- Passing string ByVal in VB.NET AND C#
- Return object from PowerShell using a parameter ("By Reference" parameter)?
- vbval vs public variable
- python 3.9 x64 ctypes pass char pointer by reference (char**) OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF
- Why can't a function with byref be converted directly to delegate?
- byval vs byref when passing object
- How to start a Backgroundworker ByRef
- In VB .net, When i use ByRef value, always occur runtime error help me
Related Questions in BYVAL
- Passing string ByVal in VB.NET AND C#
- vbval vs public variable
- Getting 1004 error on code that worked after adding new code
- byval vs byref when passing object
- Cannot delete the cells with old data in them to make way for new data
- VB.NET array parameter mechanism, byval and byref
- Pass values from BackgroundWorker DoWork to BackgroundWorker Completed
- ByVal in VBA Events
- How do you update the same cell you've entered a value in, based on another cell?
- Error: [Expression expected] using ByVal modifier
- C++ Templates ByRef vs. ByVal
- UIAutomationClient elementFromPoint With VBA error "User-Defined type may not be passed ByVal"
- Switching Byref to Byval on method calls VB.NET
- Sas how to have 2 byvar in a display
- VB.NET and calling byval/byref C# overloads
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)
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?
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 :)