Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How can I tell the shape of a Variant containing an Array?

The Variant containing an Array is an argument to VBA function.

I have a VBA function MyFunc(X). I use it directly in an Excel worksheet by
typing
=MyFunc(A1:A4) right into a cell. The argument comes into the function as a
Range. That's fine.

Excel also lets you type something like {=MyFunc(ABS(A1:A4))} (using
ctrl-shift-Enter), and in this case the argument comes into the function as a
Variant containing an array. The array is dimensioned X(4, 1). That's fine
too. I can tell if this is the case by looking at the Typename of the
argument to see if it's a Range or a Variant.

Trouble comes if the user types a _Horizontal_ range instead of a vertical
one, as in
{=MyFunc(ABS(A1:D1))} . In that case the array comes in dimensioned as
X(4), with only _One_ subscript. I can't figure out how to identify this
case, so the function crashes if I use the wrong number of subscripts to
refer to the array elements.

Driving me crazy.

Thanks in advance.

Bob
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default How can I tell the shape of a Variant containing an Array?

I found this using Google:

The number of dimensions of an array
http://www.devx.com/vb2themax/Tip/18265
Francesco Balena

Function ArrayDims(arr As Variant) As Integer
Dim i As Integer, bound As Long
On Error Resume Next
For i = 1 To 60
bound = LBound(arr, i)
If Err Then
ArrayDims = i - 1
Exit Function
End If
Next
End Function

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Bob S." wrote in message
...
The Variant containing an Array is an argument to VBA function.

I have a VBA function MyFunc(X). I use it directly in an Excel worksheet
by
typing
=MyFunc(A1:A4) right into a cell. The argument comes into the function as
a
Range. That's fine.

Excel also lets you type something like {=MyFunc(ABS(A1:A4))} (using
ctrl-shift-Enter), and in this case the argument comes into the function
as a
Variant containing an array. The array is dimensioned X(4, 1). That's
fine
too. I can tell if this is the case by looking at the Typename of the
argument to see if it's a Range or a Variant.

Trouble comes if the user types a _Horizontal_ range instead of a vertical
one, as in
{=MyFunc(ABS(A1:D1))} . In that case the array comes in dimensioned as
X(4), with only _One_ subscript. I can't figure out how to identify this
case, so the function crashes if I use the wrong number of subscripts to
refer to the array elements.

Driving me crazy.

Thanks in advance.

Bob



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default How can I tell the shape of a Variant containing an Array?

Jon Peltier wrote:
I found this using Google:

The number of dimensions of an array
http://www.devx.com/vb2themax/Tip/18265
Francesco Balena

Function ArrayDims(arr As Variant) As Integer
Dim i As Integer, bound As Long
On Error Resume Next
For i = 1 To 60
bound = LBound(arr, i)
If Err Then
ArrayDims = i - 1
Exit Function
End If
Next
End Function

- Jon


For effixiency you might want to use Dim i As Byte; and perhaps even Dim
bound As Byte, though that's more problematic.

Alan Beban
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default How can I tell the shape of a Variant containing an Array?

This will be a more efficient way to get the number of dimensions of an
array:

Option Explicit
Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (pDst As Any, _
pSrc As Any, _
ByVal ByteLen As Long)

Sub testing()

Dim arr(1 To 1, 1 To 10)

MsgBox ArrayTester(arr)

End Sub


Function ArrayTester(arr As Variant) As Integer

'-----------------------------------------------------------------
' will return:
' -1 if not an array
' 0 if an un-dimmed array
' 1 or more indicating the number of dimensions of a dimmed array
'-----------------------------------------------------------------
Dim ptr As Long
Dim VType As Integer

Const VT_BYREF = &H4000&

'get the real VarType of the argument
'this is similar to VarType(), but returns also the VT_BYREF bit
CopyMemory VType, arr, 2

'exit if not an array
If (VType And vbArray) = 0 Then
ArrayTester = -1
Exit Function
End If

'get the address of the SAFEARRAY descriptor
'this is stored in the second half of the
'Variant parameter that has received the array
CopyMemory ptr, ByVal VarPtr(arr) + 8, 4

'see whether the routine was passed a Variant
'that contains an array, rather than directly an array
'in the former case ptr already points to the SA structure.
'Thanks to Monte Hansen for this fix
If (VType And VT_BYREF) Then
' ptr is a pointer to a pointer
CopyMemory ptr, ByVal ptr, 4
End If

'get the address of the SAFEARRAY structure
'this is stored in the descriptor

'get the first word of the SAFEARRAY structure
'which holds the number of dimensions
'...but first check that saAddr is non-zero, otherwise
'this routine bombs when the array is uninitialized
'(Thanks to VB2TheMax aficionado Thomas Eyde for
' suggesting this edit to the original routine.)
If ptr Then
CopyMemory ArrayTester, ByVal ptr, 2
End If

End Function


RBS



"Jon Peltier" wrote in message
...
I found this using Google:

The number of dimensions of an array
http://www.devx.com/vb2themax/Tip/18265
Francesco Balena

Function ArrayDims(arr As Variant) As Integer
Dim i As Integer, bound As Long
On Error Resume Next
For i = 1 To 60
bound = LBound(arr, i)
If Err Then
ArrayDims = i - 1
Exit Function
End If
Next
End Function

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Bob S." wrote in message
...
The Variant containing an Array is an argument to VBA function.

I have a VBA function MyFunc(X). I use it directly in an Excel worksheet
by
typing
=MyFunc(A1:A4) right into a cell. The argument comes into the function as
a
Range. That's fine.

Excel also lets you type something like {=MyFunc(ABS(A1:A4))} (using
ctrl-shift-Enter), and in this case the argument comes into the function
as a
Variant containing an array. The array is dimensioned X(4, 1). That's
fine
too. I can tell if this is the case by looking at the Typename of the
argument to see if it's a Range or a Variant.

Trouble comes if the user types a _Horizontal_ range instead of a
vertical
one, as in
{=MyFunc(ABS(A1:D1))} . In that case the array comes in dimensioned as
X(4), with only _One_ subscript. I can't figure out how to identify this
case, so the function crashes if I use the wrong number of subscripts to
refer to the array elements.

Driving me crazy.

Thanks in advance.

Bob




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Excel Programming 6 November 9th 05 05:54 AM
Array problem: Key words-Variant Array, single-element, type mismatch error davidm Excel Programming 1 November 8th 05 04:21 AM
ReDim Object array as parameter of Variant array Peter T Excel Programming 4 May 10th 05 02:11 PM
Variant Array Steph[_3_] Excel Programming 1 June 2nd 04 01:29 AM
variant array containing cel adresses convert to actual ranges-array Peter[_21_] Excel Programming 5 December 10th 03 09:50 PM


All times are GMT +1. The time now is 02:20 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"