vba array - Extract 3 Columns

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am adding Range("A1:Z19") into Array. and expected ouput colulmns are Column 1,5,10
Below code extract first Column as output.I am doing it 3 times for 3 Columns.

Range("aa1").Resize(UBound(ar, 1)).Value = Application.Index(ar, 0, 1) This print single columns

Can we extract expected 3 columns in a single line. like Application.Index(ar, 0, Array(1,2,3) Can we print 3 columns.

Rich (BB code):
Dim ar As Variant
ar = Range("A1:Z19").value

Range("aa1").Resize(UBound(ar, 1)).Value = Application.Index(ar, 0, 1)
Range("ab1").Resize(UBound(ar, 1)).Value = Application.Index(ar, 0, 5)
Range("ac1").Resize(UBound(ar, 1)).Value = Application.Index(ar, 0, 10)


Sub Extract_specific_Columns()

Dim i As Long
Dim j As Long
Dim myArray As Variant

'Below code works Extract 3 Columns.
With Range("A1:Z19")
   ReDim myArray(1 To .Rows.Count, 1 To 3)

    For i = 1 To .Rows.Count
        myArray(i, 1) = .Cells(i, 5).Value
        myArray(i, 2) = .Cells(i, 1).Value
        myArray(i, 3) = .Cells(i, 10).Value
    Next i

End With

Range("aa1").Resize(UBound(myArray, 1), UBound(myArray, 2)).Value = Application.Index(myArray, 0, 0)

'2 Method also works, But Can we shorten the Code in Single line.

Dim ar As Variant
ar = Range("a1").CurrentRegion.Value

Range("aa1").Resize(UBound(ar, 1)).Value = Application.Index(ar, 0, 1)
Range("ab1").Resize(UBound(ar, 1)).Value = Application.Index(ar, 0, 5)
Range("ac1").Resize(UBound(ar, 1)).Value = Application.Index(ar, 0, 10)

End Sub

Thanks
mg
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
VBA Code:
   Range("AA1").Resize(UBound(Ar), 3).Value = Application.Index(Ar, Evaluate("Row(1:" & UBound(Ar) & ")"), Array(1, 5, 10))
 
Upvote 0
Rich (BB code):
Hi Fluff,

Brilliant! It worked, nice piece of code !

Can you explain below line, also how Evaluate works. thanks in advance for help,.
Application.Index(ar, Evaluate("Row(1:" & UBound(ar) & ")"), Array(10, 5, 1))


Thanks
mg
 
Upvote 0
Code tags are for code, please do NOT use them for normal text.
The evaluate returns an array of numbers from 1 to 19 (in this case), which then enables the index to return all rows.
 
Upvote 0
Hi Fluff,

Thanks once again for help. learned something new , ?

Thanks.
mg
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,711
Members
449,118
Latest member
MichealRed

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