Copying a worksheet from a workbook into another workbook

pincivma

Board Regular
Joined
Dec 12, 2004
Messages
204
I found this Excel code on the internet. It works fine. However there is a problem with the code. When I run the code a second time, I get a message that says I cannot save this workbook with the same name. Is there a code that I can use that can override the Workbook named Marios sales results. Or instead of adding a workbook is there a way to call a workbook on the 4th line of code??? Thanks

Sub SaveAsMariosSalesResults()
Application.DisplayAlerts = False
ThisFile = Sheets("Commission Table").Range("V16").Value
Set wb = Workbooks.Add

ThisWorkbook.Activate
Sheets("Mario").Copy Before:=wb.Sheets(1)
wb.Activate
wb.SaveAs Filename:=ThisFile, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Change

Code:
Set wb = Workbooks.Add

to

Code:
Set wb = Workbooks.Open(ThisFile)
 
Upvote 0

Forum statistics

Threads
1,215,569
Messages
6,125,600
Members
449,238
Latest member
wcbyers

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