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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,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