VBA Array - store visible rows into Array

Mallesh23

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

I want to autofilter on Column("a"), Criteria1:= Array("Sachin","Dhoni") and store visible rows into array.
or Without autofilter Add complete row of data to array where Criteria is met.

and Print from Array to worksheet. Want to learn array here. Thanks



Below is my attempted Code... gives correct output. But want output into Array here.

VBA Code:
 Sub Array_Help()

    Dim arr As Variant
    Dim rg As Range
    Dim coll As New Collection
    Dim sht As Worksheet
    Set sht = ThisWorkbook.Worksheets(1)
   
    arr = Join(Array("Sachin", "Dhoni"), "!")
    Set rg = sht.Range("A1").CurrentRegion
   
    Dim i As Long
    'Add Data to Dictionary
   
    For i = 2 To rg.Rows.Count
        If InStr(1, arr, rg.Cells(i, 1), vbTextCompare) > 0 Then
            coll.Add rg.Rows(i).Value
        End If
    Next i


    Dim item As Variant
    Dim row As Long
    row = 2
    Dim columns As Long
   
    'Print data
     For Each item In coll
        columns = UBound(item, 2)
        sht.Cells(row, 11).Resize(1, columns).Value = item
        row = row + 1
    Next

End Sub

Dummy Table.

Array_help.xlsm
BCDEFGHIJKLMNOPQR
1aaabbbcccdddeeefffgggExpected OutputNameaaabbbcccdddeeefffggg
2aaabbbcccdddeeefffgggSachinaaabbbcccdddeeefffggg
3aaabbbcccdddeeefffgggDhoniaaabbbcccdddeeefffggg
4aaabbbcccdddeeefffgggSachinaaabbbcccdddeeefffggg
5aaabbbcccdddeeefffgggDhoniaaabbbcccdddeeefffggg
6aaabbbcccdddeeefffgggSachinaaabbbcccdddeeefffggg
7aaabbbcccdddeeefffgggDhoniaaabbbcccdddeeefffggg
8aaabbbcccdddeeefffggg
9aaabbbcccdddeeefffggg
10aaabbbcccdddeeefffggg
11aaabbbcccdddeeefffggg
12aaabbbcccdddeeefffggg
Sheet1



Thanks
mg
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How do you know it gives the correct output? Every row is exactly the same!!
Also you have not included col A.
 
Upvote 0
Hi Fluff,

Column A is a list of Player Name, I want data of Sachin and Dhoni's data as an ouput.

Manually It will be autofilter Sachin and Dhoni in Column A, and copy visible Data into column K2 onwards.

Not complete row here, as output is in same sheet.



Thanks
mg
 
Upvote 0
But there is no column A in your data!!!!
 
Upvote 0
Hi fluff,

OMG ! my mistake, Column A was missing, Thanks for highlighting.

here is below updated data. Thanks.

Array_help.xlsm
ABCDEFGHIJKLMNOPQR
1NameaaabbbcccdddeeefffgggExpected OutputNameaaabbbcccdddeeefffggg
2SachinaaabbbcccdddeeefffgggSachinaaabbbcccdddeeefffggg
3DhoniaaabbbcccdddeeefffgggDhoniaaabbbcccdddeeefffggg
4SachinaaabbbcccdddeeefffgggSachinaaabbbcccdddeeefffggg
5DhoniaaabbbcccdddeeefffgggDhoniaaabbbcccdddeeefffggg
6VirataaabbbcccdddeeefffgggSachinaaabbbcccdddeeefffggg
7SachinaaabbbcccdddeeefffgggDhoniaaabbbcccdddeeefffggg
8Hardikaaabbbcccdddeeefffggg
9Dhoniaaabbbcccdddeeefffggg
10Kapilaaabbbcccdddeeefffggg
11Kapilaaabbbcccdddeeefffggg
12Yuvrajaaabbbcccdddeeefffggg
Sheet1


Thanks
mg
 
Upvote 0
This is totally untested, as there is no point in testing against that data.
VBA Code:
Sub Mallesh()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   Ary = Sheets("Sheet1").Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ary, 2))
   For r = 1 To UBound(Ary)
      If Ary(r, 1) = "Sachin" Or Ary(r, 1) = "Dhoni" Then
         nr = nr + 1
         For c = 1 To UBound(Ary, 2)
            Nary(nr, c) = Ary(r, c)
         Next c
      End If
   Next r
   Sheets("Sheet1").Range("K2").Resize(nr, UBound(Ary, 2)).Value = Nary
End Sub
 
Upvote 0
Hi Fluff,

Awsomeeee ......Tested and It worked as expected, Thanks for your help.?



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

Forum statistics

Threads
1,214,661
Messages
6,120,797
Members
448,994
Latest member
rohitsomani

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