Add entire row to collection and print to worksheet

Certified

Board Regular
Joined
Jan 24, 2012
Messages
189
Hi,

I have a spreadsheet with close to 6000 rows and 200 columns.

I want to pull data that meets certain criteria, place that data in a collection and paste the results on a fresh worksheet.

Right now I have the data in a collection, but I can't figure out how to print the data.

In my locals window it looks like this...
item 1
-------Item 1(1)
-----------item(1,1) "Company"

Here is the code I have so far..


VBA Code:
Sub IncludeOnlyGEMS(erMain As Long, ecMain As Long)

    Dim rngMasterLocation As Range
   
    'Start Range at row 2 to exclude Source information

    Set rngMasterLocation = Range(Sheet6.Cells(2, 1), Sheet6.Cells(erMain, ecMain))  
   
    Dim collMasterLoc As New Collection
   
    Dim i As Integer
   
    For i = 2 To erMain
   
        If Sheet6.Cells(i, 23).Value <> "" Then
       
            If Sheet6.Cells(i, 23).Value <> "GD999" Then
           
                If Sheet6.Cells(i, 23) <> "NA" Then   
                   
                    collMasterLoc.Add Sheet6.Range("A" & i & ":" & "FQ" & i).Value
           
                End If
           
            End If
       
        End If
   
    Next i

I am at a lost. Any ideas?
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
E=Rather than using a collection, why not use an array like
VBA Code:
Sub IncludeOnlyGEMS(erMain As Long, ecMain As Long)

   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   'Start Range at row 2 to exclude Source information
   
   Ary = Range(Sheet6.Cells(2, 1), Sheet6.Cells(erMain, ecMain)).Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ar, 2))
   
   For r = 2 To erMain
      If Ary(i, 23).Value <> "" And Ary(i, 23).Value <> "GD999" And Ary(i, 23) <> "NA" Then
         nr = nr + 1
         For c = 1 To UBound(Ary, 2)
            Nary(nr, c) = Ary(r, c)
         Next c
      End If
   Next r
   Sheet7.Range("A2").Resize(nr, UBound(Nary)).Value = Nary
End Sub
 
Upvote 0
Solution
E=Rather than using a collection, why not use an array like
VBA Code:
Sub IncludeOnlyGEMS(erMain As Long, ecMain As Long)

   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
  
   'Start Range at row 2 to exclude Source information
  
   Ary = Range(Sheet6.Cells(2, 1), Sheet6.Cells(erMain, ecMain)).Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ar, 2))
  
   For r = 2 To erMain
      If Ary(i, 23).Value <> "" And Ary(i, 23).Value <> "GD999" And Ary(i, 23) <> "NA" Then
         nr = nr + 1
         For c = 1 To UBound(Ary, 2)
            Nary(nr, c) = Ary(r, c)
         Next c
      End If
   Next r
   Sheet7.Range("A2").Resize(nr, UBound(Nary)).Value = Nary
End Sub
That is perfect. Thanks.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,490
Messages
6,125,094
Members
449,205
Latest member
ralemanygarcia

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