VBA Copy sheets from one Variable sheet to another

mkpippin

New Member
Joined
Jan 26, 2012
Messages
21
Hello,

I'm trying to copy two worksheets from one workbook to another. This seems to be fairly straight forward, but for some reason, I can't seem to accomplish this with a variable sheet name.

I have set one of the workbooks to "Report" and the other "Edit"

The "Edit" workbook is currently active and I'm looking to copy the "Lists" worksheet from "Edit" to "Report".

Sheets("Lists").Select
Sheets("Lists").Copy Before:=Report.Sheets("Summary")


Any idea where the break is in this code? Is it a simple Syntax error that I'm overlooking?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this

Code:
Sub Test_copy()
  Dim wb1 As Workbook, wb2 As Workbook
  Dim sh1 As Worksheet
  
  Set wb1 = Workbooks("[COLOR=#ff0000]Edit.xlsx[/COLOR]")    'source book
  Set wb2 = Workbooks("[COLOR=#ff0000]Report.xlsx[/COLOR]")  'destination book
  Set sh1 = wb1.Sheets("Lists")       'sheet to copy from source book
  
  sh1.Copy before:=wb2.Sheets("Summary")
End Sub

These elements must exist:
- "Edit.xlsx" must be open
- "Report.xlsx" must be open
- The sheet "Lists" must exist in the book Edit
- The "Summary" sheet must exist in the Report book

Change
"Edit.xlsx" and "Report.xlsx" by the names of your books.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,119
Messages
6,134,751
Members
449,887
Latest member
robyngknapp

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