Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ReDim array

Status
Not open for further replies.

Borgis

Programmer
Joined
Jun 22, 2001
Messages
5
Location
GB
I am trying to copy values from one array into a new one, however I don't know how many columns or rows are going to be needed for the new array.

I have tried to ReDim the array but it does work. Can anyone tell me what's wrong with the code.

Thanks

numrows=ubound(arrRecords,2)
numcols=ubound(arrRecords,1)
prevrep=arrRecords(0,0)
Dim arrNrec()
ReDim arrNrec(2,2)
arrNrec(0,0)=arrRecords(0,0)
For rowcounter = 0 To numrows
If prevrep <> arrRecords(0,rowcounter) Then
prevrep=arrRecords(0,rowcounter)
ncol=1
If nrow >= UBound(arrNrec,2) Then
ReDim Preserve arrNrec(ncol,nrow + 5)
End If
nrow = nrow + 1
arrNrec(0,nrow)=arrRecords(0,rowcounter)
End If
arrNrec(ncol,nrow)=arrRecords(1,rowcounter)
If ncol >= ubound(arrNrec,1) Then
ReDim Preserve arrNrec(ncol + 2,nrow)
End If
ncol=ncol+1
Next
 
Borgis, I found your code a little hard to interpret (there's no comments!), but here's a &quot;standard&quot; way of copying the contents of one array to another; it's not very elegant, but should work.

'Define the new array
Code:
Dim arrNrec()
'Define arbitrary counters
Code:
Dim a, b

'Dimension the new array to be the same as the old one
Code:
ReDim arrNrec (UBound(arrRecords,1), UBound(arrRecords,2))
'Loop through the &quot;columns&quot;
[/code]For a = LBound(arrRecords,1) To UBound(arrRecords,1)
Code:
[COLOR=green]  'For each &quot;column&quot;, loop through the &quot;rows&quot;[/color]
[code]  For b = LBound(arrRecords,2) To UBound(arrRecords,2)
'copy each &quot;cell&quot; from the old array to the new one
Code:
    arrNrec(a,b) = arrRecords(a,b)
  Next
Next

Hope this helps, Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top