Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5
Default VBA syntax help: ReDim Preserve an array

I'd like to use a multi-dimensional array to clean up some data in a
spreadsheet: each row contains data pertaining to 5 years of annual
data spread out over many columns, and I'd like to create a normalized
data table out of it. I thought I would write information to a
dimensional array, insert a new tab, and write data from the array
into the new tab.

The only twist is: a row may or may not contain data for all 5 years,
so I thought I would ReDim Preserve the array and augment the row
count by 1 when data is present. What I have is

ReDim arrData(1 To Z, 1 To 13) As String 'declaration; unknown # rows,
13 columns

for K = 1 to LastRow 'LastRow predefined in the code as the last row
of the tab
if [certain cell value for year 1] 0 then
Z = Z + 1
ReDim Preserve arrData (1 to Z), 1 to 13)
arrData(z,1) = [cell value]
etc
end if

if [certain cell value for year 2] 0 then
Z = Z + 1
ReDim Preserve arrData (1 to Z), 1 to 13)
arrData(z,1) = [cell value]
etc
end if

etc
etc
Next K

....and row 1 of the array populates, but I get a runtime error 9,
Subscript Out Of Range error in the Year 2 section of the code, and I
can't seem to debug it.

Can anyone suggest a fix?

Thanks!

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default VBA syntax help: ReDim Preserve an array

First, it's better to copy|paste from your real code.

It'll avoid syntax errors like:
ReDim Preserve arrData (1 to Z), 1 to 13)

And second, look at ReDim in VBA's help. You'll find that you can only change
the last dimension.

Dave wrote:

I'd like to use a multi-dimensional array to clean up some data in a
spreadsheet: each row contains data pertaining to 5 years of annual
data spread out over many columns, and I'd like to create a normalized
data table out of it. I thought I would write information to a
dimensional array, insert a new tab, and write data from the array
into the new tab.

The only twist is: a row may or may not contain data for all 5 years,
so I thought I would ReDim Preserve the array and augment the row
count by 1 when data is present. What I have is

ReDim arrData(1 To Z, 1 To 13) As String 'declaration; unknown # rows,
13 columns

for K = 1 to LastRow 'LastRow predefined in the code as the last row
of the tab
if [certain cell value for year 1] 0 then
Z = Z + 1
ReDim Preserve arrData (1 to Z), 1 to 13)
arrData(z,1) = [cell value]
etc
end if

if [certain cell value for year 2] 0 then
Z = Z + 1
ReDim Preserve arrData (1 to Z), 1 to 13)
arrData(z,1) = [cell value]
etc
end if

etc
etc
Next K

...and row 1 of the array populates, but I get a runtime error 9,
Subscript Out Of Range error in the Year 2 section of the code, and I
can't seem to debug it.

Can anyone suggest a fix?

Thanks!


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5
Default VBA syntax help: ReDim Preserve an array

And second, look at ReDim in VBA's help. You'll find that you can only change
the last dimension.

Thanks, Dave. Can you recommend a dynamic way create a N Rows, Y
Columns array where N is uncertain?

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default VBA syntax help: ReDim Preserve an array

Just reverse your rows and columns--both when you assign the values and when you
read the values back.

ReDim Preserve arrData (1 to 13, 1 to z)
arrData(1,z) = [cell value]




Dave wrote:

And second, look at ReDim in VBA's help. You'll find that you can only change
the last dimension.

Thanks, Dave. Can you recommend a dynamic way create a N Rows, Y
Columns array where N is uncertain?


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 783
Default VBA syntax help: ReDim Preserve an array

If the functions in the freely downloadable file at
http://home.pacbell.net/beban are available to your workbook

ResizeArray myArray, N, Y

Alan Beban

Dave wrote:
And second, look at ReDim in VBA's help. You'll find that you can only change
the last dimension.


Thanks, Dave. Can you recommend a dynamic way create a N Rows, Y
Columns array where N is uncertain?


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
How do I Preserve Borders? BeetleBailey Excel Discussion (Misc queries) 10 June 24th 07 07:59 PM
Preserve settings Bampah Excel Discussion (Misc queries) 3 March 6th 06 04:58 PM
Simplify syntax of a Sum(IF... Array JustMe602 Excel Worksheet Functions 4 October 7th 05 05:49 AM
syntax to refer to a range as an array? david cassain Excel Worksheet Functions 12 May 26th 05 11:28 PM
Want to Use an Array; Not Sure of Syntax RichK Excel Discussion (Misc queries) 4 May 6th 05 08:15 AM


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