Macro to create a new workbook from a worksheet
Closed
simonrhi
Posts
2
Registration date
Thursday 18 May 2017
Status
Member
Last seen
24 May 2017
-
21 May 2017 à 14:27
vcoolio Posts 1411 Registration date Thursday 24 July 2014 Status Contributor Last seen 6 September 2024 - 24 May 2017 à 05:13
vcoolio Posts 1411 Registration date Thursday 24 July 2014 Status Contributor Last seen 6 September 2024 - 24 May 2017 à 05:13
Related:
- How do you create a new workbook in excel
- How to create a serial number generator in excel - Guide
- Excel mod apk for pc - Download - Spreadsheets
- Create new skype account - Guide
- Arrow keys not working in excel - Guide
- How to change date format in excel - Guide
2 responses
vcoolio
Posts
1411
Registration date
Thursday 24 July 2014
Status
Contributor
Last seen
6 September 2024
262
23 May 2017 à 07:22
23 May 2017 à 07:22
Hello Simon,
Assuming that you have a name in, say, cell A1 with which you wish to name the new work book, the following code may do the task for you:-
The code will create a new work book, name it from cell A1 and copy all data from the active sheet in the current work book to the first sheet in the new work book.
You may have to change line 6 to the sheet that you are creating the new work book from.
In line 10, you will need to add the file path that you need to save the new work book to.
If the new file name is in another cell, just change the reference to cell A1 in line 10 above.
I hope that this helps.
Cheerio,
vcoolio.
Assuming that you have a name in, say, cell A1 with which you wish to name the new work book, the following code may do the task for you:-
Sub CreateNewWbk()
Dim rng As Range
Dim ws As Worksheet
Dim nFN As String
Set ws = Sheet1
Application.ScreenUpdating = False
nFN = "C:\Users\YOUR FILEPATH HERE\" & Range("A1").Value & ".xlsx"
With ws
Set rng = .UsedRange
rng.Copy
Workbooks.Add
Sheets("Sheet1").[A1].PasteSpecial xlPasteValues
Sheets("Sheet1").[A1].PasteSpecial xlPasteFormats
Sheets("Sheet1").[A1].Select
ActiveWorkbook.SaveAs Filename:=nFN
ActiveWorkbook.Close
End With
MsgBox "Done!", vbExclamation
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
The code will create a new work book, name it from cell A1 and copy all data from the active sheet in the current work book to the first sheet in the new work book.
You may have to change line 6 to the sheet that you are creating the new work book from.
In line 10, you will need to add the file path that you need to save the new work book to.
If the new file name is in another cell, just change the reference to cell A1 in line 10 above.
I hope that this helps.
Cheerio,
vcoolio.
vcoolio
Posts
1411
Registration date
Thursday 24 July 2014
Status
Contributor
Last seen
6 September 2024
262
24 May 2017 à 05:13
24 May 2017 à 05:13
Hello Simon,
Yes, no worries.
Just add the following line:-
directly after line 16 in the code above.
Let us know if that sorts it out for you.
Cheerio,
vcoolio.
Yes, no worries.
Just add the following line:-
Sheets("Sheet1").[A1].PasteSpecial xlPasteColumnWidths
directly after line 16 in the code above.
Let us know if that sorts it out for you.
Cheerio,
vcoolio.
24 May 2017 à 04:14
Thanks for this, it works really well. One thing i would like to change if possible and that is to retain the column widths from the original worksheet, do you think this possible?
Many thanks for your assistance with this.
Best regards,
Simon