Macro which Select Sheet instead of Range

vasudev_kamat

New Member
Joined
May 7, 2012
Messages
38
Hi Friends, I have a macro which is selecting range, below is the code. However my requirement is macro to copy full sheet instead of range and paste in another sheet. My data in range starting A2 to D9 in column. Row ending range keep changing. Please help.

Sub Copy_PasteSpecial_Method()

'Copy range to clipboard
Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D9").Copy

'PasteSpecial to paste values, formulas, formats, etc.
Workbooks("Reports.xlsm").Worksheets("Data").Range("A2").PasteSpecial Paste:=xlPasteValues

End Sub

Regards
Vasudev
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Maybe this
Sub Copy_PasteSpecial_Method() dim lr as long lr= sheets("Export")cells(rows.count,"D").end(xlup).row 'Copy range to clipboard Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D" & lr).Copy 'PasteSpecial to paste values, formulas, formats, etc. Workbooks("Reports.xlsm").Worksheets("Data").Range("A2").PasteSpecial Paste:=xlPasteValues End Sub
 
Upvote 0
Maybe this
Sub Copy_PasteSpecial_Method() dim lr as long lr= sheets("Export")cells(rows.count,"D").end(xlup).row 'Copy range to clipboard Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D" & lr).Copy 'PasteSpecial to paste values, formulas, formats, etc. Workbooks("Reports.xlsm").Worksheets("Data").Range("A2").PasteSpecial Paste:=xlPasteValues End Sub
Hi , It is showing compile error.

Please help
 

Attachments

  • compile error.PNG
    compile error.PNG
    56.3 KB · Views: 0
Upvote 0
OOps sorry. Missed a period

Sub MM1() dim lr as long lr= sheets("Export").cells(rows.count,"D").end(xlup).row 'Copy range to clipboard Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D" & lr).Copy 'PasteSpecial to paste values, formulas, formats, etc. Workbooks("Reports.xlsm").Worksheets("Data").Range("A2").PasteSpecial Paste:=xlPasteValues End Sub
 
Upvote 0
OOps sorry. Missed a period

Sub MM1() dim lr as long lr= sheets("Export").cells(rows.count,"D").end(xlup).row 'Copy range to clipboard Workbooks("New-Data.xlsx").Worksheets("Export").Range("A2:D" & lr).Copy 'PasteSpecial to paste values, formulas, formats, etc. Workbooks("Reports.xlsm").Worksheets("Data").Range("A2").PasteSpecial Paste:=xlPasteValues End Sub
Something wrong here bro. It is showing subscript out of range message. Even though all sheet names are correct.
 

Attachments

  • OUT OF RANGE.PNG
    OUT OF RANGE.PNG
    9.6 KB · Views: 0
Upvote 0
Something wrong here bro. It is showing subscript out of range message. Even though all sheet names are correct.
Hi Bro. Finally fixed the code.

Final one as follows. Thanks.

Sub MM1()
Dim lr As Long
lr = Workbooks("New-data.xlsx").Sheets("Export").Cells(Rows.Count, "V").End(xlUp).Row
'Copy range to clipboard
Workbooks("New-data.xlsx").Worksheets("Export").Range("A2:V" & lr).Copy

'PasteSpecial to paste values, formulas, formats, etc.
Workbooks("test.xlsm").Worksheets("Data").Range("B49").PasteSpecial Paste:=xlPasteValues

End Sub
 
Upvote 0
Are the workbook names also correct !!
Are both workbooks open ?
Try it this way

Sub MM1() Dim lr As Long lr = Sheets("Export").Cells(Rows.Count, "D").End(xlUp).Row 'Copy range to clipboard Workbooks("New-Data.xlsx"). Worksheets("Export").Range("A2:D" & lr).Copy 'PasteSpecial to paste values, formulas, formats, etc. With Workbooks("Reports.xlsm"). Worksheets("Data").Range("A2") .PasteSpecial Paste:=xlPasteValues End With End Sub
 
Upvote 0
Are the workbook names also correct !!
Are both workbooks open ?
Try it this way

Sub MM1() Dim lr As Long lr = Sheets("Export").Cells(Rows.Count, "D").End(xlUp).Row 'Copy range to clipboard Workbooks("New-Data.xlsx"). Worksheets("Export").Range("A2:D" & lr).Copy 'PasteSpecial to paste values, formulas, formats, etc. With Workbooks("Reports.xlsm"). Worksheets("Data").Range("A2") .PasteSpecial Paste:=xlPasteValues End With End Sub
Hi Michael Bro, Its fixed. I have defined workbook.

Sub MM1()
Dim lr As Long
lr = Workbooks("New-data.xlsx").Sheets("Export").Cells(Rows.Count, "V").End(xlUp).Row
'Copy range to clipboard
Workbooks("New-data.xlsx").Worksheets("Export").Range("A1:V" & lr).Copy

'PasteSpecial to paste values, formulas, formats, etc.
Workbooks("test.xlsm").Worksheets("Data").Range("B49").PasteSpecial Paste:=xlPasteValues

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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