Bugs in Sub to copy range to another sheet

XLbyname

New Member
Joined
Nov 18, 2021
Messages
11
Office Version
  1. 2010
Platform
  1. Windows
Hi All,

It's been a few years since I've written any VBA and I'm having trouble waking up again. I'm knocking my head against a brick wall trying to write what I thought would be quite a simple Sub to copy a range from one sheet and paste it (values only) to another sheet.

Here's how I want to call the Sub:

VBA Code:
Call CopyPasteValues("Your Data", "A1", 102, 13, "Results", "C1")

This means:
  • copy a range in the "Your Data" sheet that starts in A1 and is 102 rows x 13 columns
  • paste it (as values, not formulae) to "Results" sheet, starting in C1 (top left)
When I run the code below I get Run-time error '1004': Application-defined or object-defined error.

Where am I going wrong? Or does someone have a copy and paste sub ready to go that doesn't involve physically switching sheets?

Thanks very much in advance for any assistance.

VBA Code:
Sub CopyPasteValues(strSourceSheet As String, strSourceTopLeft As String, lNumRows As Long, lNumCols As Long, strDestSheet As String, strDestTopLeft As String)

    Sheets(strSourceSheet).Range(Range(strSourceTopLeft), Range(strSourceTopLeft).Offset(lNumRows - 1, lNumCols - 1)).Copy
    Sheets(strDestSheet).Range(strDestTopLeft).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try it like
VBA Code:
Sub CopyPasteValues(strSourceSheet As String, strSourceTopLeft As String, lNumRows As Long, lNumCols As Long, strDestSheet As String, strDestTopLeft As String)

    Sheets(strSourceSheet).Range(strSourceTopLeft).Resize(NumRows, lNumCols).Copy
    Sheets(strDestSheet).Range(strDestTopLeft).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
End Sub
 
Upvote 0
Would this work?
VBA Code:
Sub CopyPasteValues(strSourceSheet As String, strSourceTopLeft As String, lNumRows As Long, lNumCols As Long, strDestSheet As String, strDestTopLeft As String)
    Sheets(strDestSheet).Range(strDestTopLeft).Resize(lNumRows, lNumCols).Value = Sheets(strSourceSheet).Range("A1").Resize(lNumRows, lNumCols).Value
End Sub
 
Upvote 0
Both of those worked like a charm - thank you Fluff and Z51.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

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