[VBA] "Call"ing functions and subroutines

AiRiFiEd

New Member
Joined
Mar 3, 2014
Messages
36
I have always thought that the "Call" reserved word is optional when it comes to calling functions and subroutines.

However, I was attempting to implement my insertion sort function and, somehow, when i tried to pass the array into the function, not using the "Call" reserved word threw an error (if parameter is defined as an array - e.g. ByRef vData() as Double) or failed to update the array (if parameter is defined as variant - e.g. ByRef vData as Variant).

Any idea/explanation for such behaviour? Thank you!

Code:
Public Function fnc_SortInsertion(ByRef vData As Variant) As Boolean

    Dim i As Long
    Dim x As Long
    Dim vTemp As Variant
    
    fnc_SortInsertion = False
    
    For i = LBound(vData, 1) + 1 To UBound(vData, 1)
        vTemp = vData(i)
        x = i - 1
        While x > -1 And vData(x) > vTemp
            vData(x + 1) = vData(x)
            
            x = x - 1
        Wend
        vData(x + 1) = vTemp
    Next i

    If Err = 0 Then fnc_SortInsertion = True

End Function

Public Sub sub_testtest()
    Dim vArray(1 To 3) As Double
    
    vArray(1) = 3
    vArray(2) = 10
    vArray(3) = 5
    
    Call fnc_SortInsertion(vArray)
    Debug.Print vArray(3)

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Your function returns TRUE/FALSE
VBA needs to be told what to do with that value

This works
Code:
Debug.Print fnc_SortInsertion(vArray), vArray(3)
and this
Code:
If fnc_SortInsertion(vArray) = True Then MsgBox "All OK"
as does this
Code:
x = fnc_SortInsertion(vArray)
Debug.Print x, vArray(3)
 
Last edited:
Upvote 0
hey Yongle thanks for your reply!

but it seems changing the function to a subroutine does not help either if "Call" is not invoked.

further, how does invoking "Call" help to tell excel what to do with the return?

Thanks again!
 
Upvote 0
When Call is used to initiate a function the function's return value is discarded by VBA
 
Upvote 0
You don't need Call but, when you don't use it, don't use brackets when you aren't using the return value. So either:

Code:
Call fnc_SortInsertion(vArray)

or:

Code:
fnc_SortInsertion vArray

not:

Code:
fnc_SortInsertion (vArray)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top