PDA

View Full Version : array.copy



mferrisi
06-15-2007, 08:15 AM
Hello,

I have an array called info that is 1 to 60, 1 to 3
and I want to copy the first column to a new array, t, that is 1 to 60.
Can I use the array.copy to make this happen? If so, I can't seem to get it right.


Thank you,

mvidas
06-15-2007, 09:36 AM
Hi,

array.copy is a vb.net function, not available in VBA.

If you want to make your t() array to have the dimensions (1 to 60, 1 to 1), you can do something like:Sub mferrisiarraycopy()
Dim info(), t(), FD As Long, SD As Long
ReDim info(1 To 60, 1 To 3)

'fill in the array with dummy data
For SD = 1 To 3
For FD = 1 To 60
info(FD, SD) = 100 * SD + FD
Next
Next

'copy array
t() = info()

'remove parts of second dimension
ReDim Preserve t(1 To 60, 1 To 1)
End Sub

However, if you want t to be (1 to 60) only, you'll need to iterate through it:Sub mferrisiarraycopy()
Dim info(), t(), FD As Long, SD As Long
ReDim info(1 To 60, 1 To 3)

'fill in the array with dummy data
For SD = 1 To 3
For FD = 1 To 60
info(FD, SD) = 100 * SD + FD
Next
Next

ReDim t(1 To 60)
For FD = 1 To 60
t(FD) = info(FD, 1)
Next
End Sub

lucas
06-15-2007, 09:37 AM
I'm not sure but can't you just make them equal:
t = info

Paul_Hossler
06-16-2007, 06:35 AM
IF (you're feeling adventurous) AND _
(your data is consistant, e.g. all Long) THEN
you could use CopyMemory API to grab a column _
and copy it to your output array
ENDIF



'Be very careful with this one - you can wipe out the wrong memory and learn what GPF means
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnovba00/html/LightningStrings.asp
Sub StringSwap(ByRef S1 As String, ByRef S2 As String)
Dim HoldAddr As Long
CopyMemory HoldAddr, ByVal VarPtr(S1), 4
CopyMemory ByVal VarPtr(S1), ByVal VarPtr(S2), 4
CopyMemory ByVal VarPtr(S2), HoldAddr, 4
End Sub

Sub mferrisiarraycopy()

'source and dest must be fixed variables and of same type (Long)
Dim info() As Long, t() As Long
Dim FD As Long, SD As Long
Dim iNumBytesToCopy As Long, iNumBytesInCol As Long

ReDim info(1 To 60, 1 To 3)

'fill in the array with dummy data for demo
For SD = 1 To 3
For FD = 1 To 60
info(FD, SD) = 100 * SD + FD
Next
Next

'size dest array
ReDim t(1 To 60)

'Byte: 1
'Integer, Boolean: 2
'Long, Single: 4
'Double, Currency or Date: 8
'String, Variant, anything else - too tricky, for me anyway
'could be made more general purpose
iNumBytesToCopy = 60 * 4
iNumBytesInCol = 60 * 4

'first col
't(1) = mem loc of first long, info(1,1) = first mem loc of first col
Call CopyMemory(t(1), info(1, 1), iNumBytesToCopy)

'second col
't(1) = mem loc of first long, info(1,2) = first mem loc of second col
Call CopyMemory(t(1), info(1, 2), iNumBytesToCopy)


'third col
't(1) = mem loc of first long, info(1,3) = first mem loc of third col
Call CopyMemory(t(1), info(1, 3), iNumBytesToCopy)

Stop

End Sub

mikerickson
06-16-2007, 10:33 AM
This works for me.

Dim newArray As Variant
newArray = Application.Transpose(Application.Index(Info, 0, 1))


A 60X3 is not too large for INDEX.

Paul_Hossler
06-17-2007, 06:12 AM
:cool: Didn't know you could pull a column out like that

Only difference I saw was newArray was a Variant array containing Variants, instead of a array containing Longs, but that wouldn't a difference 99.9% of the time

Live and Learn (and hang out in the forums)