VBA for referring to new user-defined workbook

xcellrodeo

Board Regular
Joined
Oct 27, 2008
Messages
206
Hello MrExcel Community I have an issue with a VBA script. I am trying to copy a spreadsheet from one workbook to a new user-defined Workbook. The script I have is as follows:
Code:
Workbooks("CART2013.xlsm").Activate
Sheets("Sales By Month").Select
Sheets("Sales By Month").Copy Before:=Workbooks("&T2&.xlsm").Sheets(1)
However, this doesn't work.Btw, T2 refers to an input box where the new file name is declared i.e Region 1.xlsm (but file names are can change). Also I am having trouble determining the correct code for copying the worksheet into the correct position in the new workbook.At present it overwrites whatever is on the 1st sheet in the new workbook. Ideally I would want the script to be modified so that the worksheet is copied into the new workbook at the very front of whatever tabs /sheets are there (i.e not overwriting them).Hope this helps.Any help is greatly appreciated.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try

Code:
Workbooks("CART2013.xlsm").Activate
Sheets("Sales By Month").Copy Before:=Workbooks(T2 & ".xlsm").Sheets(1)
 
Upvote 0
Thanks for the suggestion but I am still having problems getting the code to work. Here is the complete script which may be more helpful.#Sub CopyRankPage45()
Set NewBook = Workbooks.Add
'User-defined file name
T2 = InputBox("Enter a file name and file extention i.e 'Data.xlsm'")
'File Path to be saved in selected location
ChDir "C:\Users\M\Desktop\CAL - RANK\test"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\M\Desktop\CAL - RANK\test\ " & T2 & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

'Copy Rank Worksheet to new Workbook
Workbooks("CART2013.xlsm").Activate
Sheets("Sales By Month").Select
Sheets("Sales By Month").Copy Before:=Workbooks(T2 & ".xlsm").Sheets(1)
'Renaming
T1 = InputBox("Enter date of issue in the format 'Mar-13'")
Sheets("Sales By Month").Name = "Issued " & T1

#
 
Upvote 0
If you are entering the complete file name including the extension try

Code:
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\M\Desktop\CAL - RANK\test\" & T2, FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Note that I alo removed a space after test\
 
Upvote 0
Thanks for your input. Did you have any suggestions on the issue described above concerning the script
Sheets("Sales By Month").Copy Before:=Workbooks(T2 & ".xlsm").Sheets(1)
which determines the position where the worksheet is placed in the new workbook? I am still having the problem that the worksheet is being copied correctly but overwrites any previous sheet in the workbook.Thanks
 
Upvote 0
This should work

Code:
Sheets("Sales By Month").Copy Before:=Workbooks(T2).Sheets(1)
 
Upvote 0
The above script does work but only for the first copy into a blank workbook. For example lets say the worksheet is named "Sales Apr-13". Lets say that the following month, another sales data worksheet with the name of "Sales May-13" is copied into the same workbook. Sales Apr-13 will now be overwritten with Sales May-13.I think this is because the following script specifies that any new worksheets are copied to the position immediately before sheets 1.So I would imagine the script would need to be modified so that any new worksheet is copied not before sheet 1 but before the last or first sheet in the range and to ensure that no sheet is overwritten if that makes sense. I look forward to reading some suggestions :)
 
Upvote 0
I can't see any reason why your code should be overwriting anything, 'copy before' should do exactly that not overwrite or replace anything.</SPAN>

The only thing I can think of is that your code is creating a new workbook every time instead of opening the existing one once it has been created the first time. So what's happening is you're creating a new workbook, copying in the latest sheet then saving over the existing workbook, so it looks like you are overwriting it. Does that make sense?</SPAN>
 
Upvote 0

Forum statistics

Threads
1,203,174
Messages
6,053,908
Members
444,694
Latest member
JacquiDaly

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