Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Peter Rooney
 
Posts: n/a
Default How to declare a dynamic array

I want to declare an array, the size of which depends on the length of the
contents of a cell - so, if the cell contains 123456, I want to declare a 6
element array etc.
However, when I try to run the following code, I'm told that I have to use a
constant, as against an expression, when defining the array size.
Can anyone suggest a way around this, please?

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray(ArrayLength) As Integer
End Sub

Thanks in advance for your help

Pete


  #2   Report Post  
Duke Carey
 
Posts: n/a
Default

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray() As Integer
ReDim StoreArray(ArrayLength)
End Sub

"Peter Rooney" wrote in message
...
I want to declare an array, the size of which depends on the length of the
contents of a cell - so, if the cell contains 123456, I want to declare a
6
element array etc.
However, when I try to run the following code, I'm told that I have to use
a
constant, as against an expression, when defining the array size.
Can anyone suggest a way around this, please?

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray(ArrayLength) As Integer
End Sub

Thanks in advance for your help

Pete




  #3   Report Post  
Peter Rooney
 
Posts: n/a
Default

Thanks, Duke - much appreciated!

Pete



"Duke Carey" wrote:

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray() As Integer
ReDim StoreArray(ArrayLength)
End Sub

"Peter Rooney" wrote in message
...
I want to declare an array, the size of which depends on the length of the
contents of a cell - so, if the cell contains 123456, I want to declare a
6
element array etc.
However, when I try to run the following code, I'm told that I have to use
a
constant, as against an expression, when defining the array size.
Can anyone suggest a way around this, please?

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray(ArrayLength) As Integer
End Sub

Thanks in advance for your help

Pete





  #4   Report Post  
Dave Peterson
 
Posts: n/a
Default

If you don't have
Option Base 1
at the top of your module, then StoreArray will be from 0 to arraylength.

You could be more explicit:

Option Explicit
Sub StringProcess()
Dim ArrayLength As Integer
Dim StoreArray() As Integer
ArrayLength = Len(ActiveCell.Value)
If ArrayLength 0 Then
ReDim StoreArray(1 To ArrayLength)
Else
'what should happen
MsgBox "Empty activecell"
End If
End Sub



Duke Carey wrote:

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray() As Integer
ReDim StoreArray(ArrayLength)
End Sub

"Peter Rooney" wrote in message
...
I want to declare an array, the size of which depends on the length of the
contents of a cell - so, if the cell contains 123456, I want to declare a
6
element array etc.
However, when I try to run the following code, I'm told that I have to use
a
constant, as against an expression, when defining the array size.
Can anyone suggest a way around this, please?

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray(ArrayLength) As Integer
End Sub

Thanks in advance for your help

Pete



--

Dave Peterson
  #5   Report Post  
Peter Rooney
 
Posts: n/a
Default

Dave, A good tip with some useful error trapping!

Thanks a lot!

Pete



"Dave Peterson" wrote:

If you don't have
Option Base 1
at the top of your module, then StoreArray will be from 0 to arraylength.

You could be more explicit:

Option Explicit
Sub StringProcess()
Dim ArrayLength As Integer
Dim StoreArray() As Integer
ArrayLength = Len(ActiveCell.Value)
If ArrayLength 0 Then
ReDim StoreArray(1 To ArrayLength)
Else
'what should happen
MsgBox "Empty activecell"
End If
End Sub



Duke Carey wrote:

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray() As Integer
ReDim StoreArray(ArrayLength)
End Sub

"Peter Rooney" wrote in message
...
I want to declare an array, the size of which depends on the length of the
contents of a cell - so, if the cell contains 123456, I want to declare a
6
element array etc.
However, when I try to run the following code, I'm told that I have to use
a
constant, as against an expression, when defining the array size.
Can anyone suggest a way around this, please?

Sub StringProcess()
Dim ArrayLength As Integer
ArrayLength = Len(ActiveCell.Value)
Dim StoreArray(ArrayLength) As Integer
End Sub

Thanks in advance for your help

Pete



--

Dave Peterson

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
Can't create dynamic charts Brian Sells Charts and Charting in Excel 7 March 22nd 05 04:23 AM
Match / Vlookup within an Array formula Hari Prasadh Excel Discussion (Misc queries) 3 February 3rd 05 04:37 PM
Where is the bug in my array? Gail Gurman Excel Discussion (Misc queries) 1 January 25th 05 12:36 AM
Formula to list unique values JaneC Excel Worksheet Functions 4 December 10th 04 12:25 AM
VBA Import of text file & Array parsing of that data Dennis Excel Discussion (Misc queries) 4 November 28th 04 10:20 PM


All times are GMT +1. The time now is 03:30 PM.

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

About Us

"It's about Microsoft Excel"