Copy and paste from one worksheet into a new workbook and save to a particular directory

bantam999

New Member
Joined
Jul 24, 2024
Messages
2
Office Version
  1. 2003 or older
Hi

I have cobbled together from several sources code that hopefully copies a range ("AA1:AN1000") from an open worksheet/ workbook and opens a new blank workbook and copies and pastes as formats & values into the new workbook, saves this to a set directory and closes this file.

This is what I have with the line that is causing me issues in red:-

Sub CreateNewWorkbookWithValues()
Dim dirPath, fName As String, newWb As Workbook, thisWb As Workbook
Set thisWb = ThisWorkbook
dirPath = thisWb.Path
fName = ActiveSheet.Range("M2").Value
Application.ScreenUpdating = False
Set newWb = Workbooks.Add
thisWb.Sheets("Summary").Range("AA1:AN1000").Copy newWb.Sheets("Sheet1").Range("A1")
newWb.SaveAs Filename:=NewBook.Worksheets("Sheet1").Range("M1").Value
newWb.Close
Application.ScreenUpdating = True
MsgBox "Workbook created at:" & vbCrLf & vbCrLf & dirPath & "\" & fName
End Sub

It opens a new worksheet/ workbook, copies and pastes but then cannot save the file.

Also, how would I change the code to save it to a particular folder and not just the folder that the original file is saved in

Thanks in advance
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Welcome to the Board!

Note the line returning the error, and the part I put in red:
Rich (BB code):
newWb.SaveAs Filename:=NewBook.Worksheets("Sheet1").Range("M1").Value
What exactly is this "NewBook" variable?
I do not see you defining any such variable in your code.
You have "newWb", but not "NewBook".
 
Upvote 0
Welcome to the Board!

Note the line returning the error, and the part I put in red:
Rich (BB code):
newWb.SaveAs Filename:=NewBook.Worksheets("Sheet1").Range("M1").Value
What exactly is this "NewBook" variable?
I do not see you defining any such variable in your code.
You have "newWb", but not "NewBook".
Thanks - I've changed it to this but still does not like the same line - (apologies I am a relative novice with VBA)

Sub CreateNewWorkbookWithValues()
Dim dirPath, fName As String, newWb As Workbook, thisWb As Workbook
Set thisWb = ThisWorkbook
dirPath = thisWb.Path
fName = ActiveSheet.Range("M2").Value
Application.ScreenUpdating = False
Set newWb = Workbooks.Add
thisWb.Sheets("Summary").Range("AA1:AN1000").Copy newWb.Sheets("Sheet1").Range("A1")
newWb.SaveAs Filename:=NewBook.Worksheets("Sheet1").Range("M1").Value
newWb.Close
Application.ScreenUpdating = True
MsgBox "Workbook created at:" & vbCrLf & vbCrLf & dirPath & "\" & fName
End Sub

The network folder I want it to be saved into is :- "H:\***\***\***\***\working on"

Thanks

Bantam
 
Upvote 0
Thanks - I've changed it to this but still does not like the same line - (apologies I am a relative novice with VBA)
You did not update the code you posted - you are still showing/using the undefined "NewBook" reference.
Rich (BB code):
Sub CreateNewWorkbookWithValues()
Dim dirPath, fName As String, newWb As Workbook, thisWb As Workbook
Set thisWb = ThisWorkbook
dirPath = thisWb.Path
fName = ActiveSheet.Range("M2").Value
Application.ScreenUpdating = False
Set newWb = Workbooks.Add
thisWb.Sheets("Summary").Range("AA1:AN1000").Copy newWb.Sheets("Sheet1").Range("A1")
newWb.SaveAs Filename:=NewBook.Worksheets("Sheet1").Range("M1").Value
newWb.Close
Application.ScreenUpdating = True
MsgBox "Workbook created at:" & vbCrLf & vbCrLf & dirPath & "\" & fName
End Sub

Please be sure to use Code Tags when posting your code in the future - it makes it much easier to read.
See: How to Post Your VBA Code
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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