View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Per Jessen Per Jessen is offline
external usenet poster
 
Posts: 1,533
Default Can't get length of a variant array passed to a subroutine

Hi Craig

If you want to determine how many elements that has been loaded into your
array, I think this is what you need:

Dim HRColHdrs(3) As Variant

Private Sub HdrChk(ByVal ShtName As String, ByRef ColHdrs As Variant)
Dim i As Long, n As Long
For i = LBound(ColHdrs) To UBound(ColHdrs)
If ColHdrs(i) < "" Then
n = n + 1
End If
Next
Debug.Print n & " elements are in this array"
End Sub

Regards,
Per

"Craig Remillard" skrev i meddelelsen
...
I have a string array, declared by:

Dim HRColHdrs(3) As Variant

I explicitly define the strings, then pass them to a function, as
"ColHdrs" shown below.

Private Sub HdrChk(ByVal ShtName As String, ByRef ColHdrs As Variant)
Dim i, n As Long

Debug.Print (Len(ColHdrs))

End Sub

Within this function, I would like to determine how many elements are in
this array that was passed. However, when I use the len() function, I get
Type mismatch error.

Things I have tried:
-Declaring "HRColHdrs" as an array of type String in the main function (no
change in error)
-Declaring the "ColHdrs" as a variable of type String or Object rather
than Variant
-Declaring "ColHdrs" as a ByVal variable
-Using the ColHdrs.Length method (error changes to "Object required")
-I used debug.print to make sure that I can read the individual elements
of ColHdrs in the subroutine - I can.

Oh, if only every language handled arrays like Matlab scripting :-/.

The only workaround I can think of is to step through a For loop and
address each element of the array directly, until it throws a subscript
error, then use the error handler to extract from the loop.

EggHeadCafe - Software Developer Portal of Choice
ASP.NET 2.0: Using the Menu Control
http://www.eggheadcafe.com/tutorials...ng-the-me.aspx