Converting 2D arrays to 1D

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I'm using .GetRows to move a recordset into an array in order to use the Filter command,
However the resultant array uses array(0, n) notation whereas Filter needs array(n) notation

In Googling around for ways to convert the array I came across this:


If you are grabbing a singe row (with multiple columns) then use:

Securities = application.transpose(application.transpose _
(Worksheets(3).Range("A8:A" & SymbolCount).Value))

If you are grabbing a single column (with multiple rows) then use:

Securities = application.transpose(Worksheets(3).Range("A8:A" & SymbolCount).Value)

So, basically you just transpose twice for rows and once for columns.


It looks a nice solution, but I can't quite work out to apply it the 2D array.

Is is possible ?
Many thanks, ABB
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
try this code
Code:
Sub a()
n = 10
Dim arr(), arr1()
arr = Range("A1:A" & n).Value
arr1 = Application.Transpose(arr)
For i = 1 To 10
Debug.Print arr1(i);
Next
Debug.Print
End Sub
 
Upvote 0
Hi Alexander

Post
- the logic you'd use to convert the 2D array to the 1D array
- a sample of a 2D array and the result array you'd get using your logic
 
Last edited:
Upvote 0
Thanks for replying everyone
Patel, your example is so close. Maybe I can tweak it. You get

arr(1,1) "One"
arr(2,1) "Two"
arr(3,1) "Three"

becoming

arr1(1) "One"
arr1(2) "Two"
arr1(3) "Three"

The Format I have is

MyArray(0,1) "One"
MyArray(0,2) "Two"
MyArray(0,3) "Three"

wanting to turn that into the same as arr1.

Andrew one element is all zeros and can be discarded, if that makes sense.
PGC, hope that shows enough.
 
Last edited:
Upvote 0
The Format I have is

MyArray(0,1) "One"
MyArray(0,2) "Two"
MyArray(0,3) "Three"

wanting to turn that into the same as arr1.

PGC, hope that shows enough.

It seems you want the first row of the array

If that's the case, try:

Code:
Dim arr1 As Variant
    
    arr1 = Application.WorksheetFunction.Index(MyArray, 1, 0)
 
Upvote 0
Code:
option base 1
Sub aa()

Dim arr(3, 1), arr1()
arr(1, 1) = "One"
arr(2, 1) = "Two"
arr(3, 1) = "Three"

arr1 = Application.Transpose(arr)
For i = 1 To 3
Debug.Print arr1(i);
Next
Debug.Print
End Sub
 
Upvote 0
As pgc01 says:

Code:
Sub Test()
    Dim MyArray(0 To 1, 1 To 3)
    Dim NewArray
    Dim i As Long
    MyArray(0, 1) = "One"
    MyArray(0, 2) = "Two"
    MyArray(0, 3) = "Three"
    MyArray(1, 1) = 0
    MyArray(1, 2) = 0
    MyArray(1, 3) = 0
    NewArray = Application.Index(MyArray, 1, 0)
    For i = LBound(NewArray) To UBound(NewArray)
        MsgBox NewArray(i)
    Next i
End Sub
 
Upvote 0
Hi Folks, many thanks for the help bit I cannot get any of three to work without Type Mismatch error.
It'll be interesting to see why....

My code is

Code:
Dim  test1 As Variant, test2 As Variant, test3 As Variant, MyArray As Variant
Set ep = New ADODB.Recordset
On Error GoTo NoTable
ep.Open "SELECT Serial, Lyrics FROM tbl" & Yr, cnn, adOpenKeyset, adLockReadOnly
MyArray = ep.GetRows
ep.Close
Set ep = Nothing

test1 = Application.Transpose(MyArray)

test2 = Application.WorksheetFunction.index(MyArray, 1, 0)

test3 = Application.index(MyArray, 1, 0)

Stop
Exit Function

NoTable:
If Err = -2147217865 Then
        Set ep = Nothing
    Else
        Resume Next
End If
Is is something to do with the properties of MyArray which I cannot Dim in Advance as it's size varies depending on the recordset.

Also if it helps MyArray shows as a Watch as
http://www.britburn.co.uk/files/Image1.jpg
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top