Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
kdw kdw is offline
external usenet poster
 
Posts: 13
Default Checking existence of array dimensions

Is there any way to check for how many dimensions an array have? For
example, I can use ubound(TempArray,1), ubound(TempArray,2), etc. But what
if there is no 2nd dimension. My code would fail. I would not want to loop
through the column dimension if there isn't any so I need some way to check
for it.

Many thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Checking existence of array dimensions

Hi kdw,

kdw wrote:
Is there any way to check for how many dimensions an array have? For
example, I can use ubound(TempArray,1), ubound(TempArray,2), etc.
But what if there is no 2nd dimension. My code would fail. I would
not want to loop through the column dimension if there isn't any so I
need some way to check for it.


I don't know of any built-in way to get the dimension count, but here's a
quick function that should do the job:

Public Function gnCountDimensions(rvArray As Variant) As Integer
Dim n As Integer
Dim lTemp As Long
Dim bFoundEnd As Boolean

Do While Not bFoundEnd
On Error Resume Next
lTemp = UBound(rvArray, n + 1)
If Err.Number Then
bFoundEnd = True
Else
n = n + 1
End If
On Error GoTo 0
Loop

gnCountDimensions = n
End Function

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Checking existence of array dimensions

There's a KB article (213273) on this that says that Excel has no way to
tell you the number of dimensions and recommends that you use error handling
to do it. The following function will tell you how many dimensions and
array has:

Sub TestDimensions()
Dim a(2, 5, 7) As Integer

Debug.Print cArrayDimensions(a)
End Sub

Function cArrayDimensions(a) As Integer
Dim cDimensions As Integer
Dim iDimension As Integer
Dim x

cDimensions = 0

On Error GoTo GotDimensions
Do
x = UBound(a, cDimensions + 1)
cDimensions = cDimensions + 1
Loop

GotDimensions:

On Error GoTo 0
cArrayDimensions = cDimensions

End Function


"kdw" wrote in message
...
Is there any way to check for how many dimensions an array have? For
example, I can use ubound(TempArray,1), ubound(TempArray,2), etc. But

what
if there is no 2nd dimension. My code would fail. I would not want to

loop
through the column dimension if there isn't any so I need some way to

check
for it.

Many thanks.



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
Checking for the existence of a worksheet Allen Excel Discussion (Misc queries) 1 January 20th 10 06:57 PM
Vba - Checking existence of file ajliaks[_26_] Excel Programming 1 August 11th 04 06:43 PM
Checking for existence of excel Dave Peterson[_3_] Excel Programming 3 June 26th 04 05:11 AM
Checking for existence of excel keepITcool Excel Programming 0 June 24th 04 06:07 PM
Checking Number of Dimensions In Array DigableP Excel Programming 2 February 28th 04 11:18 PM


All times are GMT +1. The time now is 04: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"