Consulting

Results 1 to 15 of 15

Thread: Solved: Calling a Function, of Sorts

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Calling a Function, of Sorts

    Is there a way to call specifically designated lines of code within a function from another function. For example:

    [VBA]
    Sub Testing()

    ''''' SOME CODE HERE '''''

    Test:
    msgbox "This is a Test"

    End Sub
    [/VBA]

    [VBA]
    Sub Testing2()

    Call Testing.Test

    End Sub
    [/VBA]

    I know that's not the way to do it, if it is even possible.

  2. #2
    VBAX Regular
    Joined
    Jul 2010
    Posts
    23
    Location
    Yeah there are a couple ways you can do this. You will need to work with functions instead of sub routines as they do not allow you to pass variables between sub routines.

    Here are a couple different functions depending on you want to use the information:

    Here is the main sub-routine that calls two different functions and passes a variable to them.

    [vba]Sub MyTest()

    ' Pass value A to function and msgbox from
    ' the PassToMe function

    PassToMe ("A")

    ' Pass value B to PassThenReturn return string
    ' value and assign it to a variable then
    ' msgbox the variable content

    MyReturnVal = PassThenReturn("B")
    MsgBox MyReturnVal

    End Sub
    [/vba]
    Once in the function you can put whatever logic you want to check if a certain condition is met.

    Function: PassToMe requires a string to be passed to it in order to proceed. You can then take that string value and compare the value to determine what code will actually execute.

    [vba]Function PassToMe(var1 As String)

    If var1 = "A" Then
    ' put code block here
    MsgBox "In A Code Block"
    End If

    If var1 = "B" Then
    ' put code block here
    MsgBox "In B Code Block"
    End If

    End Function[/vba]
    Function: PassThenReturn requires a string to be passed to it in order to proceed and will return a string. Again you can then take that string value and compare the value to determine what code will actually execute.

    [vba]Function PassThenReturn(var1 As String) As String

    If var1 = "A" Then
    ' put code block here
    PassThenReturn = "In A Code Block"
    End If

    If var1 = "B" Then
    ' put code block here
    PassThenReturn = "In B Code Block"
    End If

    End Function
    [/vba]
    Hope that helps..

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by EDGE
    You will need to work with functions instead of sub routines as they do not allow you to pass variables between sub routines.
    Wherever do you get that idea from?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Jul 2010
    Posts
    23
    Location
    Sorry about that I Misspoke! Should have stated only Functions can return values and that values can be passed to both Subs and Functions.

  5. #5
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Thanks. I just reread my original question, and I realized that I asked about a function; whereas, I meant to say subroutine. I was just curious as to whether a named bookmark (or whatever "Test:" would be called) within the subroutine could be accessed by reference from another subroutine.

    I'll have to try to digest the function idea.

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    That way leads to spaghetti code.
    It might be cleaner if the subroutine were re-written as a seperate sub, to be called when needed.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by EDGE
    Sorry about that I Misspoke! Should have stated only Functions can return values and that values can be passed to both Subs and Functions.
    You are mistaken about that too, Subs can return values.

    [vba]

    Sub Caller()
    Dim val1 As String

    val1 = "original value"
    MsgBox val1

    Call called(val1)
    MsgBox val1

    End Sub

    Sub called(ByRef passedval As String)

    passedval = "changed value"
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    That's not really returning a value though; that's altering a passed value.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You think! So APIs dn't return a value either?

    And doesn't a function alter a passed value?

    [vba]

    MyVal = MyFunc()
    [/vba]

    MyVal will have a value at the start, even if empty, but will have a different value after, so in that sense a passed value is changed, just as with a sub. IT is only teh syntax that differs.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by xld
    You think!
    Sometimes, yes.
    So APIs dn't return a value either?
    Depends. API functions do, subs don't.
    And doesn't a function alter a passed value?

    [vba]

    MyVal = MyFunc()
    [/vba]

    MyVal will have a value at the start, even if empty, but will have a different value after, so in that sense a passed value is changed, just as with a sub. IT is only teh syntax that differs.
    So on that basis, if I use:
    [vba]MyVal = 5[/vba]

    5 is altering a passed value? MyVal is not passed to the function, the return value of the function (essentially a variable) is assigned to the MyVal variable. Again, if you wrote:
    [vba]
    MyVal = 4
    MyOtherVal = 5
    MyVal = MyOtherVal
    [/vba]
    would you really argue that MyOtherVal is changing a passed value?

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Aflatooon
    Depends. API functions do, subs don't.
    Nonsense, the API declaration frequently calls the parameter a return code. That sounds like return to me.

    Quote Originally Posted by Aflatooon
    So on that basis, if I use:
    [vba]MyVal = 5[/vba]

    5 is altering a passed value? MyVal is not passed to the function, the return value of the function (essentially a variable) is assigned to the MyVal variable.
    What function? You are assigning a variable to a value. No function involved.

    Quote Originally Posted by Aflatooon
    Again, if you wrote:
    [vba]
    MyVal = 4
    MyOtherVal = 5
    MyVal = MyOtherVal
    [/vba]
    would you really argue that MyOtherVal is changing a passed value?
    As before there is not a function or sub in sight, it is just assigning a value to a variable. I would argue that the variable is being modified, and if you wanted you can say that is changing a passed value, but I wouldn't.

    I feel you have must be having a bad day and are looking to pick an argument on nothing. It doesn't change my point that subs can return a value.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You said:
    Quote Originally Posted by xld
    And doesn't a function alter a passed value?

    [vba]

    MyVal = MyFunc()
    [/vba]

    MyVal will have a value at the start, even if empty, but will have a different value after, so in that sense a passed value is changed, just as with a sub. IT is only teh syntax that differs.
    which seems to be you saying that MyVal is passed to MyFunc. Therefore, my analogy was:
    [vba]MyVal = 5[/vba]

    By your reasoning, MyVal is being passed to 5.

    Hence I then disagree with:
    I would argue that the variable is being modified, and if you wanted you can say that is changing a passed value, but I wouldn't.
    It seems to me you just did say that.

    And I'm actually having a fun day, thanks.

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Aflatoon

    By your reasoning, MyVal is being passed to 5.
    Well, that line of reasoning (fabrication?) is total tosh, so there is no point continuing this at all.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    VBAX Regular
    Joined
    Jul 2010
    Posts
    23
    Location
    XLD I appreciate the feedback and glad that you pointed out that there are alternative ways to achieve a similar results.

    Thanks

  15. #15
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by mikerickson
    That way leads to spaghetti code.
    It might be cleaner if the subroutine were re-written as a seperate sub, to be called when needed.
    You're probably right. Thanks.

Posting Permissions

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