Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default 2D array of strings problem

I have the following erroneous subroutine:

'=================================
Sub DoStuff (A as Integer, B as Integer)

Dim (A, B) as String ' This creates an error!

End Sub
'================================

Is it possible to create a 2D array whose size depends
on the values of A and B above? I'm basically
looking for a way to create a 2D array whose
dimensions are not known at runtime. The size
of this 2D array will stay constant for the duration
of the "DoStuff" procedure, so I only need to set
it's size once.

Any ideas?

- Robert


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 279
Default 2D array of strings problem

In message of Fri, 18
Feb 2011 23:18:12 in microsoft.public.excel.programming, Robert Crandal
writes
I have the following erroneous subroutine:

'=================================
Sub DoStuff (A as Integer, B as Integer)

Dim (A, B) as String ' This creates an error!


You have not named that array
Dim X(A, B) as String ' Gets no error


End Sub
'================================

Is it possible to create a 2D array whose size depends
on the values of A and B above? I'm basically
looking for a way to create a 2D array whose
dimensions are not known at runtime. The size
of this 2D array will stay constant for the duration
of the "DoStuff" procedure, so I only need to set
it's size once.

Any ideas?

- Robert



--
Walter Briscoe
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default 2D array of strings problem

On Feb 18, 10:18*pm, "Robert Crandal" wrote:
Sub DoStuff (A as Integer, B as Integer)
Dim (A, B) as String * ' This creates an error!
End Sub

[....]
Is it possible to create a 2D array whose size depends
on the values of A and B above?


Yes: use ReDim instead Dim. For example:

Sub doit1(a As Long, b As Long)
ReDim s(a, b) As String
MsgBox "a=" & a & " b=" & b & Chr(10) & _
LBound(s, 1) & ":" & UBound(s, 1) & _
" " & LBound(s, 2) & ":" & UBound(s, 2)
End Sub

Sub callit1()
doit1 12, 34
doit1 23, 45
End Sub

However, in that form, the lower bound depends on the Option Base. It
would be prudent to specify it explicitly, for example:

ReDim s(1 to a, 1 to b) As String


The size of this 2D array will stay constant for the duration
of the "DoStuff" procedure, so I only need to set it's size once.


So place the ReDim statement early in the procedure.

However, if you mean that you want to set the size only on the first
call and retain the original size on subsequent calls even if a and b
are different, you could do the following:

Sub doit2(a As Long, b As Long)
Static first As Long
Static s 'must be Variant type
If first = 0 Then
'first call only
first = 1: ReDim s(a, b) As String
s(1, 1) = "s(1,1)": s(1, 2) = "s(1,2)"
s(2, 1) = "s(2,1)": s(2, 2) = "s(2,2)"
End If
MsgBox "a=" & a & " b=" & b & Chr(10) & _
LBound(s, 1) & ":" & UBound(s, 1) & _
" " & LBound(s, 2) & ":" & UBound(s, 2) & _
Chr(10) & _
":" & s(1, 1) & ":" & s(1, 2) & ":" & _
s(2, 1) & ":" & s(2, 2) & ":"
End Sub

Sub callit2()
doit2 12, 34
doit2 23, 45
End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default 2D array of strings problem

Thanks, that worked perfectly.

BTW, is it possible to have a 2D array dimensioned
like this:

ReDim (0 to A, 0 to 0) ' Is this possible?

I'm thinking that the second element will usually contain
ONE element, but it will be at index "0".



"joeu2004" wrote in message
...
On Feb 18, 10:18 pm, "Robert Crandal" wrote:
Sub DoStuff (A as Integer, B as Integer)
Dim (A, B) as String ' This creates an error!
End Sub
[....]
Is it possible to create a 2D array whose size depends
on the values of A and B above?

Yes: use ReDim instead Dim. For example:

Sub doit1(a As Long, b As Long)
ReDim s(a, b) As String
MsgBox "a=" & a & " b=" & b & Chr(10) & _
LBound(s, 1) & ":" & UBound(s, 1) & _
" " & LBound(s, 2) & ":" & UBound(s, 2)
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default 2D array of strings problem

You can actually disregard my 2nd question, cuz apparently it
works, but I might not use that after all.

Thank you for your help!


"Robert Crandal" wrote in message
...
Thanks, that worked perfectly.

BTW, is it possible to have a 2D array dimensioned
like this:

ReDim (0 to A, 0 to 0) ' Is this possible?

I'm thinking that the second element will usually contain
ONE element, but it will be at index "0".




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,059
Default 2D array of strings problem

On Feb 18, 11:09*pm, Walter Briscoe
wrote:
Robert Crandal writes
Sub DoStuff (A as Integer, B as Integer)
Dim (A, B) as String * ' This creates an error!


You have not named that array
Dim X(A, B) as String ' Gets no error


Until you try to call it. Then you get the runtime error "compile
error: constant expression required".
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
REARRANGE STRINGS IN ARRAY !!!! jay dean Excel Programming 3 March 30th 10 02:18 PM
Passing an array of strings from VBA to C Francogrex Excel Programming 1 January 23rd 10 10:41 PM
Creating an array of strings John Excel Programming 1 March 1st 09 02:51 AM
Array of Strings Zone Excel Discussion (Misc queries) 3 July 7th 06 04:52 PM
Counting Strings in an Array ExcelMonkey[_190_] Excel Programming 9 March 16th 05 09:21 AM


All times are GMT +1. The time now is 03:16 AM.

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"