Simple copy/paste with worksheet variables

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
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:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Do you have any merged cells, protected cells, or hidden rows on either sheet?
 
Upvote 0
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.
 
Upvote 0
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).
 
Upvote 0
addrow = 2, r = 3045, s = 11 as expected.
 
Upvote 0
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
 
Upvote 0
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)
?
 
Upvote 0
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.
 
Upvote 0
Solution
That worked! Thanks. With much gratitude. Manually copying isn't an option since the script will run on sheets with thousands of rows.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

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