Save a range of cells to replace a file

raj08536

Active Member
Joined
Aug 16, 2007
Messages
322
Office Version
  1. 365
Platform
  1. Windows
I created successfully a new file using the code below. But when I try to replace the file with new data, it ask me to debug.

Sub CreateFAIDMaster()

Dim dirPath, fName As String, newWb As Workbook, thisWb As Workbook
Set thisWb = ThisWorkbook
dirPath = thisWb.Path
fName = ActiveSheet.Range("m1").Value
Application.ScreenUpdating = False
Set newWb = Workbooks.Add
thisWb.Sheets("FAIDMaster").Range("A1:C50").Copy newWb.Sheets("Sheet1").Range("A1")
newWb.SaveAs Filename:=dirPath & "\" & fName
newWb.Close
Application.ScreenUpdating = True
MsgBox "Workbook created at:" & vbCrLf & vbCrLf & dirPath & "\" & fName

End Sub

Please change the code to replace the existing file with new data.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Thanks for responding....

Error
1675192240831.png


1675192279367.png
 
Upvote 0
When exactly does this error pop up. If you get a message pop up that says the file already exists and you want to replace it?

If you say no, then the error will occur.

Add Application.DisplayAlerts = False

VBA Code:
Dim dirPath As String, fName As String, newWb As Workbook, thisWb As Workbook
Set thisWb = ThisWorkbook
dirPath = thisWb.Path
fName = ActiveSheet.Range("m1").Value
Application.ScreenUpdating = False
Set newWb = Workbooks.Add
thisWb.Sheets("FAIDMaster").Range("A1:C50").Copy newWb.Sheets("Sheet1").Range("A1")
Application.DisplayAlerts = False
newWb.SaveAs Filename:=dirPath & "\" & fName
newWb.Close
Application.ScreenUpdating = True
MsgBox "Workbook created at:" & vbCrLf & vbCrLf & dirPath & "\" & fName
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,107
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