PC Review


Reply
Thread Tools Rate Thread

Can't get length of a variant array passed to a subroutine

 
 
Craig Remillard
Guest
Posts: n/a
 
      22nd Nov 2009
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
 
Reply With Quote
 
 
 
 
Rainer Bielefeld
Guest
Posts: n/a
 
      22nd Nov 2009
Hi Craig,

try Debug.Print (cstr(Ubound(ColHdrs)))

Regards,

Rainer


<Craig Remillard> schrieb im Newsbeitrag news:(E-Mail Removed)...
>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


 
Reply With Quote
 
Charles Williams
Guest
Posts: n/a
 
      22nd Nov 2009
Debug.Print (UBound(ColHdrs)-LBound(ColHdrs)+1)

Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com

<Craig Remillard> wrote in message
news:(E-Mail Removed)...
>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
>



 
Reply With Quote
 
Per Jessen
Guest
Posts: n/a
 
      22nd Nov 2009
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
news:(E-Mail Removed)...
>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


 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      22nd Nov 2009
As ColHdrs is declared as a variant array you can simply do:

Debug.Print Application.WorksheetFunction.CountA(ColHdrs)

So, no need for the loop.


RBS


"Per Jessen" <(E-Mail Removed)> wrote in message
news:unw%(E-Mail Removed)...
> 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
> news:(E-Mail Removed)...
>>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

>


 
Reply With Quote
 
Craig Remillard
Guest
Posts: n/a
 
      23rd Nov 2009
Thanks to everyone for your replies. Once I had UBound and LBound it was easy. For reference, here is the (much less elegant) solution I had originally come up with using error handlers. I worked, but ugh.

===========================

Private Function HdrChk(ByVal ShtName As String, ByVal ColHdrs As Variant) As Boolean
Dim i, d As Long

'set an offset value in case lbound <> 1
d = 1 - LBound(ColHdrs)

'fill top row with header labels
For i = LBound(ColHdrs) To UBound(ColHdrs)
Worksheets(ShtName).Cells(1, i + d).Value = ColHdrs(i)
Next i

'return true. This boolean is currently a programming
'artifact, but it has been kept in case some error
'handling is desired.
HdrChk = True
Exit Function
End Function

Private Function XHdrChkX(ByVal ShtName As String, ByVal ColHdrs As Variant) As Boolean
Dim i As Long

On Error GoTo ErrHdlr

i = 1
Do Until True
Worksheets(ShtName).Cells(1, i).Value = ColHdrs(i)
i = i + 1
Loop

LoopExit:

XHdrChkX = True

Exit Function

ErrHdlr:
If Err = 9 Then
GoTo LoopExit
Else
XHdrChkX = False
Exit Function
End If


End Function



Per Jessen wrote:

Hi CraigIf you want to determine how many elements that has been loaded into
22-Nov-09

Hi Crai

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

Dim HRColHdrs(3) As Varian

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

Regards
Pe

"Craig Remillard" skrev i meddelelsen

Previous Posts In This Thread:

EggHeadCafe - Software Developer Portal of Choice
SQL Injection - SQL Server Convert EXEC To Avoid Attacks
http://www.eggheadcafe.com/tutorials...sql-serve.aspx
 
Reply With Quote
 
Craig Remillard
Guest
Posts: n/a
 
      23rd Nov 2009
Sorry, that was both the function that I ended up using (HdrChk), and the original function with error handler loop escape (XHdrChkX). The latter now goes to the graveyard!



Craig Remillard wrote:

Solution taken
23-Nov-09

Thanks to everyone for your replies. Once I had UBound and LBound it was easy. For reference, here is the (much less elegant) solution I had originally come up with using error handlers. I worked, but ugh.

===========================

Private Function HdrChk(ByVal ShtName As String, ByVal ColHdrs As Variant) As Boolean
Dim i, d As Long

'set an offset value in case lbound <> 1
d = 1 - LBound(ColHdrs)

'fill top row with header labels
For i = LBound(ColHdrs) To UBound(ColHdrs)
Worksheets(ShtName).Cells(1, i + d).Value = ColHdrs(i)
Next i

'return true. This boolean is currently a programming
'artifact, but it has been kept in case some error
'handling is desired.
HdrChk = True
Exit Function
End Function

Private Function XHdrChkX(ByVal ShtName As String, ByVal ColHdrs As Variant) As Boolean
Dim i As Long

On Error GoTo ErrHdlr

i = 1
Do Until True
Worksheets(ShtName).Cells(1, i).Value = ColHdrs(i)
i = i + 1
Loop

LoopExit:

XHdrChkX = True

Exit Function

ErrHdlr:
If Err = 9 Then
GoTo LoopExit
Else
XHdrChkX = False
Exit Function
End If


End Function

Previous Posts In This Thread:

EggHeadCafe - Software Developer Portal of Choice
Map Stored Procedure Output To Class Properties
http://www.eggheadcafe.com/tutorials...dure-outp.aspx
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Microsoft Excel Programming 7 11th Jun 2011 12:01 AM
Use the value of a parameter passed to a subroutine to update a re David Microsoft Access Form Coding 4 10th Apr 2008 03:48 PM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Microsoft Excel Programming 1 8th Nov 2005 04:21 AM
Variant Array Length? =?Utf-8?B?VGF0YWthdQ==?= Microsoft Access VBA Modules 4 31st Aug 2005 04:19 PM
variant array containing cel adresses > convert to actual ranges-array Peter Microsoft Excel Programming 5 10th Dec 2003 09:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:35 AM.