View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Test if a variable is given as array?

I am probably missing something but why not simply -

I think you're right. I've had my version in my ArrayUtilities module
for so long that I don't even think about it.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Sat, 18 Apr 2009 16:50:37 +0100, "Peter T" <peter_t@discussions
wrote:

I am probably missing something but why not simply -

Function IsArrayAllocated(V As Variant) As Boolean
On Error Resume Next
IsArrayAllocated = (LBound(V, 1) <= UBound(V, 1))
End Function

IOW, in what scenario would the bounds check return true but IsArray(V)
return false.

Regards,
Peter T

"Chip Pearson" wrote in message
.. .

if you want to test for an initialized array better to use the method


In my standard library of functions, I use the following function to
test whether a variable is an array and if so whether it has been
allocated. The function will work with any type of array with any
number of dimensions.

Function IsArrayAllocated(Arr As Variant) As Boolean
On Error Resume Next
IsArrayAllocated = IsArray(Arr) And _
Not IsError(LBound(Arr, 1)) And _
LBound(Arr, 1) <= UBound(Arr, 1)
End Function

' Call with
Dim B As Boolean
B = IsArrayAllocated(V)
If B = True Then
Debug.Print "V is an array"
Else
Debug.Print "V is not an allocated array"
End If

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Sat, 18 Apr 2009 15:06:13 +0100, "Peter T" <peter_t@discussions
wrote:

Use ISARRAY

That's certainly one way but there are many ways. Depends on the
objective,
if you want to test for an initialized array better to use the method
suggested by GS

Regards,
Peter T


"joel" wrote in message
...
Use ISARRAY

Dim MyArray(1 To 5) As Integer, YourArray, MyCheck ' Declare array
variables.
YourArray = Array(1, 2, 3) ' Use Array function.
MyCheck = IsArray(MyArray) ' Returns True.
MyCheck = IsArray(YourArray)

"Gary''s Student" wrote:

Since arrays have Ubounds and simple variables do not, we can test for
this:

Sub qwerty(t As Variant)
On Error GoTo NotArray
x = UBound(t)
MsgBox (x)
Exit Sub
NotArray:
MsgBox ("clearly not an array")
End Sub


Sub routine()
Dim inputt As String
inputt = "A"
Call qwerty(inputt)
End Sub

--
Gary''s Student - gsnu200847


"Charlotte E" wrote:

Hello,


Is it possible to test if a variable is given as array?

I need to pass on a value to an UDF in VBA, but it must be given as
an
array, i.e.:

MyVar = Array("A")

rather than:

MyVar = "A"


Is it possible to test if 'MyVar' is an array?


TIA,

CE