Passing a variant array to a subroutine that turns out to be null

Mackeral

Board Regular
Joined
Mar 7, 2015
Messages
249
Office Version
  1. 365
Platform
  1. Windows
Variant_Array(1) "Line 1"
Variant_Array(2) = "LIne 2"

Call Var_Add(Variant_Array,,"Line 3", "Line 4") ' "Variant_Array" has good data in it before the call.
.
VBA Code:
Function Var_Add(Variant_Array() As Variant, ParamArray LINES()) As Variant
  
    Knt = UBound(Variant_Array)
    
    For Each Line In LINES
        Knt = Knt + 1
        ReDim Preserve Variant_Array(Knt)
        Variant_Array(Knt) = Line
    Next Line
    
    Var_Add = Variant_Array
    
End Function ' Var_Add

My problem is that "UBound(Variant_Array)" always returns a 0, or another way of stating the problem is that "Variant_Array" doesn't contain anything after the call.

What am I missing? & thanks for your help.
Mac
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This seems to run just fine?

VBA Code:
Sub foobar()
    Dim Variant_array()
    ReDim Variant_array(2)
    Variant_array(0) = "Line 1"
    Variant_array(1) = "LIne 2"

    Call Var_Add(Variant_array, , "Line 3", "Line 4")    ' "Variant_Array" has good data in it before the call.
End Sub
Function Var_Add(Variant_array() As Variant, ParamArray LINES()) As Variant
  Dim knt As Long
    Dim line As Variant
   
    knt = UBound(Variant_array)
   
    For Each line In LINES
        knt = knt + 1
        ReDim Preserve Variant_array(knt)
        Variant_array(knt) = line
    Next line
   
    Var_Add = Variant_array
   
End Function
 
Upvote 0
Actually, I'm surprised you're not getting a subscript out of range error, since it looks like you declare a 1-based array and you redim preserve using a 0-based array. In any case, try the following...

VBA Code:
Sub test()

    ReDim Variant_Array(1 To 2) As Variant
    Dim i As Long

    Variant_Array(1) = "Line 1"
    Variant_Array(2) = "Line 2"
    
    Call Var_Add(Variant_Array, "Line 3", "Line 4")
    
    For i = LBound(Variant_Array) To UBound(Variant_Array)
        Debug.Print Variant_Array(i)
    Next i
    
End Sub

Function Var_Add(Variant_Array() As Variant, ParamArray LINES() As Variant) As Variant

    Dim Knt As Long
    Dim Line As Variant
    
    Knt = UBound(Variant_Array)
    
    For Each Line In LINES
        Knt = Knt + 1
        ReDim Preserve Variant_Array(1 To Knt)
        Variant_Array(Knt) = Line
    Next Line
    
    Var_Add = Variant_Array
    
End Function ' Var_Add

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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