vba expained

Peterfc2

Active Member
Joined
Jan 2, 2004
Messages
404
Office Version
  1. 2013
Platform
  1. Windows
Code:
With Sheets("uniques")
    Dim sh As Worksheet
    For Each sh In ThisWorkbook.Sheets
    If sh.Name Like "*-*" Then
        sh.Range("B10", sh.Cells(Rows.Count, 2).End(xlUp).Offset(-3)).Copy Sheets("uniques").Cells(Rows.Count, 1).End(xlUp)(2)
    End If
Next
End With

My sheets are seasons in the format 2019-20

This has worked fine but now give run time error 1004

Code:
sh.Range("B10", sh.Cells(Rows.Count, 2).End(xlUp).Offset(-3)).Copy Sheets("uniques").Cells(Rows.Count, 1).End(xlUp)(2)

Can someone break this row down. I have sheets with no data I think this could be the problem.
Cheers
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
When a worksheet is empty, sh.Cells(Rows.Count, 2).End(xlUp) will refer to B1. And so when you try to offset this range upwards by 3 rows, you'll get an error. Therefore, you should first find the last used row in Column B, and then decide whether to copy/paste.

By the way, in your example, you're looping through the Sheets collection. However, if your workbook contains a sheet other than a worksheet (ie. Chart sheet), you'll get an error. Therefore, I would suggest you loop through the Worksheets collection instead.

Let's say that you want to copy the data only if data exists in cells B10 and beyond. In this case, your code can be re-written as follows...

Code:
    Dim sh As Worksheet
    Dim lastRow As Long
    
    For Each sh In ThisWorkbook.Worksheets
        With sh
            If .Name Like "*-*" Then
                lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
                If lastRow >= 10 Then
                    .Range("B10:B" & lastRow).Copy Sheets("uniques").Cells(Sheets("uniques").Rows.Count, 1).End(xlUp)(2)
                End If
            End If
        End With

    Next sh


Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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