Run-time error ("Wrong number of arguments...)

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
I'm all of a sudden getting a Run-time error ("Wrong number of arguments ...) with this code.

How do I fix this.

VBA Code:
Sub test()
'
'
' Keyboard Shortcut: Ctrl+Shift+P
'
With ActiveSheet
    .Range("A:D" & Rows.Count, "L" & Rows.Count, "P" & Rows.Count, "T" & Rows.Count).End(xlUp).Copy
End With
    Workbooks.Add
    
    Worksheets("Sheet1").Paste
    
    With ActiveSheet
        Range("H1") = "Deleted?"
    
    End With
    
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Yep, you have some issues with the construction of this line here:
Rich (BB code):
   .Range("A:D" & Rows.Count, "L" & Rows.Count, "P" & Rows.Count, "T" & Rows.Count).End(xlUp).Copy

I find it better to build the range as a string first so you can easily using a MsgBox to verify that you construcuted it correctly, like this:
VBA Code:
Sub test()
'
'
' Keyboard Shortcut: Ctrl+Shift+P
'
Dim s As String

s = "A" & Rows.Count & ":D" & Rows.Count & ",L" & Rows.Count & ", P" & Rows.Count & ", T" & Rows.Count

With ActiveSheet
    .Range(s).End(xlUp).Copy
End With
    Workbooks.Add
   
    Worksheets("Sheet1").Paste
   
    With ActiveSheet
        Range("H1") = "Deleted?"
   
    End With
   
End Sub
If that does not do what you need, please explain to us (in plain English) exactly what it is you are trying to accomplish.
It is kind of off to place the ".End(xlUp)" part on a whole range of disjointed columns.
It is more common to do it on one column, and apply that value to all other range references afterwards.
If we need to go that route, we just need you to tell us which column we can look at to determine the last row of data.
 
Upvote 0
I am trying to copy columns A:D, L, P, and T from one workbook and paste into a new workbook.
 
Upvote 0
Did my updated code do what you want?
What column can we use in your original data to determine exactly where the last row of data resides?
 
Upvote 0
Did my updated code do what you want?
What column can we use in your original data to determine exactly where the last row of data resides?
Sorry, but no, I didn't.
It only copied the last row of column A and pasted it in A1 of the new workbook. And assigned H1
 
Upvote 0
Ok, we really need you to be clearer about what you are trying to do and answer all the questions asked.
Remember, we know nothing about your project or your data structure. We only have to work with whatever you decide to share with us.

So I will ask for a third time:
What column can we use in your original data to determine exactly where the last row of data resides?

And regarding this:
I am trying to copy columns A:D, L, P, and T from one workbook and paste into a new workbook.
Are you trying to copy them to the SAME columns?
Because I believe that by default. Excel will paste column L right after column D, leaving no blanks in between.

If you are having a hard time describing, maybe post some images of what your data looks like, and what you want your expected result to look like.
 
Upvote 0
I am trying to copy all the data in Columns A:D, L, P and T and paste it into a new workbook in the 1st 5 columns, like this:





1636558519187.png
 
Upvote 0
Thank you. That helps clarify things.

Try this:
VBA Code:
Sub test()
'
'
' Keyboard Shortcut: Ctrl+Shift+P
'
    Dim lr As Long
    Dim s As String

'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row

'   Build range string
    s = "A1:D" & lr & ",L1:L" & lr & ",P1:P" & lr & ",T1:T" & lr

'   Copy data
    With ActiveSheet
        .Range(s).Copy
    End With
    
    Workbooks.Add
    
    Worksheets("Sheet1").Paste
    
    With ActiveSheet
        Range("H1") = "Deleted?"
    End With
    
End Sub
 
Upvote 0
Solution
Thank you. That helps clarify things.

Try this:
VBA Code:
Sub test()
'
'
' Keyboard Shortcut: Ctrl+Shift+P
'
    Dim lr As Long
    Dim s As String

'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row

'   Build range string
    s = "A1:D" & lr & ",L1:L" & lr & ",P1:P" & lr & ",T1:T" & lr

'   Copy data
    With ActiveSheet
        .Range(s).Copy
    End With
   
    Workbooks.Add
   
    Worksheets("Sheet1").Paste
   
    With ActiveSheet
        Range("H1") = "Deleted?"
    End With
   
End Sub
That Worked!
Thanks
 
Upvote 0
You are welcome.
I hope it makes sense how I apporached it.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,887
Members
449,057
Latest member
Moo4247

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