VBA variable passed to function is modified

Viewed 1299

I have passed a variable to a VBA function as an argument but have only referenced the argument in the function, not the original variable. It seems that the original variable is being modified by the function, meaning that it shares the same address space? What is happening here and how do i prevent it from happening?

Here is the function: (units, tens, hundreds and thousands are global integer variables)

Function Components(qty As Integer, stringSize As Integer)

If qty < stringSize Then
    units = qty

ElseIf qty >= stringSize * 100 Then
    thousands = qty \ (stringSize * 100)
    qty = qty - (thousands * stringSize * 100)
    Components qty, stringSize


ElseIf qty >= stringSize * 10 Then
    hundreds = qty \ (stringSize * 10)
    qty = qty - (hundreds * stringSize * 10)
    Components qty, stringSize

ElseIf qty >= stringSize Then
    tens = qty \ (stringSize)
    qty = qty - (tens * stringSize)
    Components qty, stringSize

End If

End Function

And i call it from another function using

Components charQty, 26

Where charQty = 565 when it is passed as an argument to Components and charQty = 19 after components has completed. I determined this by printing the values immediately before and after the function call.

I am fairly new to VBA. Any help would be greatly appreciated.

1 Answers
Related