Consulting

Results 1 to 8 of 8

Thread: VBA Naming Convention; Parameter; ByVal & ByRef;

  1. #1
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Question VBA Naming Convention; Parameter; ByVal & ByRef;

    Hi all.
    Very simple questions here. Hope all can post their experience!

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

    What is difference btw ByVal and ByRef?

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

    Thanks.

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Have you looked in the VBA help files at all?

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by sheeeng
    Hi all.
    Very simple questions here. Hope all can post their experience!

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

    What is difference btw ByVal and ByRef?

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

    Thanks.
    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.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  4. #4
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Thanks!

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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/)

  6. #6
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    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".

  7. #7
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Thumbs up Thank You!

    Thanks for all your comments.

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

    But thanks to all who really make the effort to contribute to this thread. 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.

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

    Thanks!

  8. #8
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Lightbulb

    Hello again.

    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.

    VBScript Coding Conventions (MSDN)

    http://msdn.microsoft.com/library/de...onventions.asp

    Hope can benefit all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •