PDA

View Full Version : [SOLVED] VBA Naming Convention; Parameter; ByVal & ByRef;



sheeeng
06-09-2005, 10:43 PM
Hi all. :hi: :hi:
Very simple questions here. Hope all can post their experience! :yes

Anyone know how to pass parameter of any objects (pls list all you know)? :doh:
eg. pass Range to a sub.

What is difference btw ByVal and ByRef? :doh:

What is the formal naming convention for VBA (all objects)? :doh:

Thanks. :beerchug:

Norie
06-10-2005, 05:12 AM
Have you looked in the VBA help files at all?

MWE
06-10-2005, 08:11 AM
Hi all. :hi: :hi:
Very simple questions here. Hope all can post their experience! :yes

Anyone know how to pass parameter of any objects (pls list all you know)? :doh:
eg. pass Range to a sub.

What is difference btw ByVal and ByRef? :doh:

What is the formal naming convention for VBA (all objects)? :doh:

Thanks. :beerchug:
There are many questions here. Most are the kind of questions that are best (initially) sought from VBA's help system or from a search of the KB area of this forum.

sheeeng
06-10-2005, 08:36 AM
Thanks!

Zack Barresse
06-10-2005, 09:24 AM
Well, since nobody else wants to answer ...

Question 1:
Passing a range to a sub routine can be precarious, much like a UDF. Here is an example...


Option Explicit

Sub StartProcedure()
EndProcedure ActiveCell
End Sub

Sub EndProcedure(MyRng As Range)
MsgBox "The passed range is from:" & vbNewLine & _
MyRng.Parent.Name & "!" & MyRng.Address
End Sub

Question 2:
ByVal and ByRef are referring (in VBA) that to which an argument has been passed, by a Value or by a Reference. Although when used within/with Objects, ByVal refers to an Object (e.g. the Target in a Worksheet level event). Commonly when declaring variables, the ByRef is used. It's like when using Ranges, no need to declare it as ActiveSheet.Range, because it's implied; so is ByRef. So the following two examples are the same ...


Sub Test1(x As Long)
Msgbox x
End Sub

Sub Test2(ByRef x As Long)
Msgbox x
End Sub

ByRef will change the variable which will be retained. This means you can pass variables back and forth between routines and retain the changes.

The help files on this are pretty shabby - at best. A google search will yield more results. Take an hour of free time and Google-it, just read and read and read. There's stuff out there, it's just hard to find sometimes. ;)

Question 3:
As far as a "formal naming convention", I'm not sure I understand what you mean. Are you referring to variables? If so, do a search for the popular Hungarian type notation. The MSDN Library is full of useful information such as this. (http://msdn.microsoft.com/)

BlueCactus
06-10-2005, 09:45 AM
To clarify question 2 a little, say you had:


Sub Level1()
Dim A as Integer
A = 2
Call Level2(A)
MsgBox A
End Sub

Sub Level2(ByRef AA As Integer)
AA = AA * AA
End Sub


All changes to the ByRef variable (AA) are reflected in the corresponding variable (A) in the calling proc. This is because AA is merely a reference to A. Hence, this code will give MsgBox "4".

If you change ByRef to ByVal, a new variable (AA) is created with its initial value set to the corresponding variable (A). Because AA is a new variable, its value is not reflected in A. That code would end in MsgBox "2".

sheeeng
06-11-2005, 04:06 AM
Thanks for all your comments. :cloud9:

I feel very down after no answer. :( :think: This question may seem simple to experts, not to us, the beginners. :doh:

But thanks to all who really make the effort to contribute to this thread. :thumb I just want to make this forum more informative to beginner such as me. This thread hope can compile answer from all and benefit all. :friends:

If anyone have anything to add to this thread, pls do so. :yes We are willing to hear from you. :)

Thanks!
:beerchug:

sheeeng
06-11-2005, 04:18 AM
Hello again. :hi:

I found some new information here.

Optimize string handling in Visual Basic 6.0
http://www.aivosto.com/vbtips/stringopt.html
(Now you know why vbNullValue is better than "")

Visual Basic String Examples
http://www.example-code.com/vb/string.asp
(Some information on string manipulation)

Now here is the answer to my 3rd questions. :thumb

VBScript Coding Conventions (MSDN)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vbsCodingConventions.asp

Hope can benefit all. :friends: