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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,215,084
Messages
6,123,029
Members
449,092
Latest member
ikke

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