VBA loop to collect data from all sheets

CalRich1023

New Member
Joined
Mar 15, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I need to search through all the sheets in my workbook and gather data from a range of cells, and return that to a "Master" sheet. The range will run from cells "J9:M9", but will vary in amount of rows for each sheet (maybe 1 row, maybe 100+ rows). Obviously I'd like to omit searching on the "Master" sheet also.

Could anybody help me with this?
 
Try changing this line
VBA Code:
Lastrowa = .Cells(Rows.Count, "J").End(xlUp).Row
to
VBA Code:
Lastrowa = .Range("J:J").Find("*", , xlValues, , xlByRows, xlPrevious, , , False).Row
It says:

Runtime Error 91:
Object variable or With Block Variable not set.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Do you have any sheets that might have nothing in col J, other than a formula that returns ""
 
Upvote 0
Do you have any sheets that might have nothing in col J, other than a formula that returns ""
Yes, that was going to be my next step in this process is skipping those sheets. Is that what could cause this problem?
 
Upvote 0
This is beyond my knowledgebase.
I will continue to monitor this thread to see what I can learn.
Upon further thought, would it be possible to build this so that it only takes data above a certain number? As I mentioned, the first parameter I'm looking at is an account number, which are all above 50000. So could it build built saying that, "If column J > 50000, then retrieve/copy data from columns K:M?
 
Upvote 0
How about
VBA Code:
Sub Copy_My_Range()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Range
Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1

For i = 2 To Sheets.Count
    Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1

    With Sheets(i)
         Set Lastrowa = .Range("J:J").Find("*", , xlValues, , xlByRows, xlPrevious, , , False)
         If Not Lastrowa Is Nothing Then
            .Range("J9:M" & Lastrowa.Row).Copy Sheets("Master").Cells(Lastrow, 1)
         End If
    End With
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
How about
VBA Code:
Sub Copy_My_Range()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Range
Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1

For i = 2 To Sheets.Count
    Lastrow = Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Row + 1

    With Sheets(i)
         Set Lastrowa = .Range("J:J").Find("*", , xlValues, , xlByRows, xlPrevious, , , False)
         If Not Lastrowa Is Nothing Then
            .Range("J9:M" & Lastrowa.Row).Copy Sheets("Master").Cells(Lastrow, 1)
         End If
    End With
Next
Application.ScreenUpdating = True
End Sub
This works perfectly, however I cannot seem to get it to skip the sheets I need skipped. This is what I modified and it still seems to pull data from these sheets. What did I do wrong?

VBA Code:
Sub Copy_My_Range()

Application.ScreenUpdating = False
Worksheets("MASTER").Range("A1:H1000").Clear
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Range
Dim Ws As Worksheet

Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1
 
    For i = 2 To Sheets.Count
  
        Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1
  
        With Sheets(i)
          For Each Ws In Worksheets
                If Ws.Name <> "SEARCH" And Ws.Name <> "ACCT #'S" And Ws.Name <> "DATA" And _
                    Ws.Name <> "Table Of Contents" And Ws.Name <> "COPY TEMPLATE" _
                    And Ws.Name <> "NOTES" And Ws.Name <> "MASTER" Then      '''SKIP THESE SHEETS
                  
                Set Lastrowa = .Range("J:J").Find("*", , xlValues, , xlByRows, xlPrevious, , , False)
                  
                    If Not Lastrowa Is Nothing Then
                        .Range("J9:M" & Lastrowa.Row).Copy Sheets("MASTER").Cells(Lastrow, 1)
                    End If
                End If
            Next Ws
        End With
    Next

End Sub
 
Upvote 0
Try it like
VBA Code:
Sub Copy_My_Range()

Application.ScreenUpdating = False
Worksheets("MASTER").Range("A1:H1000").Clear
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Range

Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1
 
    For i = 2 To Sheets.Count
  
        Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1
  
        With Sheets(i)
          
                If .Name <> "SEARCH" And .Name <> "ACCT #'S" And .Name <> "DATA" And _
                    .Name <> "Table Of Contents" And .Name <> "COPY TEMPLATE" _
                    And .Name <> "NOTES" And .Name <> "MASTER" Then      '''SKIP THESE SHEETS
                  
                Set Lastrowa = .Range("J:J").Find("*", , xlValues, , xlByRows, xlPrevious, , , False)
                  
                    If Not Lastrowa Is Nothing Then
                        .Range("J9:M" & Lastrowa.Row).Copy Sheets("MASTER").Cells(Lastrow, 1)
                    End If
                End If
        End With
    Next

End Sub
 
Upvote 0
Solution
Try it like
VBA Code:
Sub Copy_My_Range()

Application.ScreenUpdating = False
Worksheets("MASTER").Range("A1:H1000").Clear
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Range

Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1

    For i = 2 To Sheets.Count
 
        Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1
 
        With Sheets(i)
         
                If .Name <> "SEARCH" And .Name <> "ACCT #'S" And .Name <> "DATA" And _
                    .Name <> "Table Of Contents" And .Name <> "COPY TEMPLATE" _
                    And .Name <> "NOTES" And .Name <> "MASTER" Then      '''SKIP THESE SHEETS
                 
                Set Lastrowa = .Range("J:J").Find("*", , xlValues, , xlByRows, xlPrevious, , , False)
                 
                    If Not Lastrowa Is Nothing Then
                        .Range("J9:M" & Lastrowa.Row).Copy Sheets("MASTER").Cells(Lastrow, 1)
                    End If
                End If
        End With
    Next

End Sub
THIS WORKS PERFECTLY! THANK YOU SO MUCH AGAIN!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
I like using select case like this:
VBA Code:
Sub Copy_My_Range()
'Modified  4/4/2021  12:38:04 AM  EDT
Application.ScreenUpdating = False
Worksheets("MASTER").Range("A1:H1000").Clear
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Range

Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1
 
    For i = 2 To Sheets.Count
  
        Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row + 1
  
        With Sheets(i)
            Select Case Sheets(i).Name
                    Case "SEARCH", "ACCT #'S", "DATA", "Table Of Contents", "COPY TEMPLATE", "NOTES", "MASTER"
                    'Do Nothing
                  
                  Case Else
                Set Lastrowa = .Range("J:J").Find("*", , xlValues, , xlByRows, xlPrevious, , , False)
                  
                    If Not Lastrowa Is Nothing Then
                        .Range("J9:M" & Lastrowa.Row).Copy Sheets("MASTER").Cells(Lastrow, 1)
                    End If
                End Select
        End With
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,969
Messages
6,175,680
Members
452,667
Latest member
vanessavalentino83

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