Simple copy/paste with worksheet variables

jardenp

Active Member
Joined
May 12, 2009
Messages
367
My copy/paste in the "problem line" below is returning a run time error 1004 Method 'Range' of object '_ Worksheet' failed.

What am I doing wrong? I know that the If statement is true for the first time at row 3045 and that the msgboxes return the correct sheet names when I have those lines uncommented.

Thanks!

VBA Code:
    Dim OWS As Worksheet
        Set OWS = ActiveWorkbook.ActiveSheet
        'MsgBox OWS.Name
    RowS("1:1").copy
    Sheets.Add After:=Sheets(Sheets.Count)
    RowS("1:1").Select
    ActiveSheet.Paste
    ActiveSheet.Name = "AR Transaction Lines"
    Dim ARTL As Worksheet
        Set ARTL = ActiveWorkbook.ActiveSheet
        'MsgBox ARTL.Name
       
    Dim r As Integer 'row counter
    Dim s As Integer '"Split" column
        s = 11
    Dim addrow As Integer 'row counter for adding lines on AR sheet
        addrow = 2
           
    For r = 2 To 3047 'OWS.Range("B1000000").End(xlUp).row

        If OWS.Cells(r, s) = "1120 · Accounts Receivable" Then
            OWS.Range(Cells(r, 1), Cells(r, 13)).copy ARTL.Range(Cells(addrow, 1), Cells(addrow, 13)) '****Problem line****
            addrow = addrow + 1
        End If
       
    Next r
 
Last edited by a moderator:

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,492
Office Version
  1. 365
Platform
  1. Windows
Do you have any merged cells, protected cells, or hidden rows on either sheet?
 

jardenp

Active Member
Joined
May 12, 2009
Messages
367
Joe, I don't think so. I don't see any. When I select all cells the merge cells button doesn't highlight. I tried unhide for all columns and rows on the original and ran the code and got the same result.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,492
Office Version
  1. 365
Platform
  1. Windows
If you hit debug when you get that error, try going to that line of code and hovering over the "addrow", "r", and "s" variables to see what they all are.
Then, go to your datasheet, and check out that row and make sure that there isn't any issue with it (errors, other odd occurrences, etc).
 

jardenp

Active Member
Joined
May 12, 2009
Messages
367

ADVERTISEMENT

addrow = 2, r = 3045, s = 11 as expected.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,492
Office Version
  1. 365
Platform
  1. Windows
Try this:
VBA Code:
    Dim OWS As Worksheet
        Set OWS = ActiveWorkbook.ActiveSheet

    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "AR Transaction Lines"
    
    Dim ARTL As Worksheet
        Set ARTL = ActiveWorkbook.ActiveSheet
    
    OWS.Rows(1).Copy ARTL.Range("A1")

    Dim r As Integer 'row counter
    Dim s As Integer '"Split" column
        s = 11
    Dim addrow As Integer 'row counter for adding lines on AR sheet
        addrow = 2
           
    For r = 2 To 3047 'OWS.Range("B1000000").End(xlUp).row

        If OWS.Cells(r, s) = "1120 · Accounts Receivable" Then
            OWS.Range(Cells(r, 1), Cells(r, 13)).Copy ARTL.Cells(addrow, 1)
            addrow = addrow + 1
        End If
       
    Next r
Note you could also avoid the loop by using Filters instead.
See: Dynamically Filter Data from One Worksheet to Another in Microsoft Excel 2010
 

jardenp

Active Member
Joined
May 12, 2009
Messages
367

ADVERTISEMENT

I still get the same error on the same line. Any idea why
VBA Code:
OWS.Rows(1).Copy ARTL.Range("A1")
works but not
VBA Code:
OWS.Range(Cells(r, 1), Cells(r, 13)).Copy ARTL.Cells(addrow, 1)
?
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,492
Office Version
  1. 365
Platform
  1. Windows
It works for me. What happens if you qualify each instance of "Cells"?, i.e.
VBA Code:
OWS.Range(OWS.Cells(r, 1), OWS.Cells(r, 13)).Copy ARTL.Cells(addrow, 1)

If that still does not work, try manually copying the first thirteen columns of a row on your OWS sheet and pasting it to your other sheet?
Does that work? Can you do it manually?

By the way, please update your profile so that we can see which version of Excel you are working with.
 
Solution

jardenp

Active Member
Joined
May 12, 2009
Messages
367
That worked! Thanks. With much gratitude. Manually copying isn't an option since the script will run on sheets with thousands of rows.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,492
Office Version
  1. 365
Platform
  1. Windows
That worked! Thanks. With much gratitude.
You are welcome.

Manually copying isn't an option since the script will run on sheets with thousands of rows.
No, I was not suggesting that as a solution. I just wanted you to test it to see if there was some issue preventing you from copy/pasting.
It is a debugging technique.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,009
Messages
5,622,149
Members
415,881
Latest member
tasic89

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
Top