Dear Folks,
I am trying to save a file with different name based on a cell value however could not save and face an error - "Runtime Error - 1004, Method 'SaveAs' of Object _Workbook' failed
I have created this macro in File A. There is an value in Sheet 2 Cell B2. code opens a new file, saves data from file A. but when it saves, give error. Below is the code which I am running. Please help with the correction
Facing error while saving the file and then closing the file
I am trying to save a file with different name based on a cell value however could not save and face an error - "Runtime Error - 1004, Method 'SaveAs' of Object _Workbook' failed
I have created this macro in File A. There is an value in Sheet 2 Cell B2. code opens a new file, saves data from file A. but when it saves, give error. Below is the code which I am running. Please help with the correction
Code:
'File A is ThisWorkbook
Set NwBook = Workbooks.Add
ThisWorkbook.Activate
Sheet2.Select
Range("B1:J1").Select
ActiveCell.Offset(0, 0).Select
Do Until ActiveCell.EntireRow.Hidden = False
ActiveCell.Offset(0, 0).Select
Loop
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
NwBook.Activate
ActiveSheet.Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.EntireColumn.AutoFit
Range("J:N").Select
Selection.EntireColumn.Delete
Range("A1").Select
Range("J1").Value = "Unique Data_" & Range("A2").Value
ActiveSheet.Name = "Unique Data"
Dim xWs As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In Application.ActiveWorkbook.Worksheets
If xWs.Name <> "Unique Data" Then
xWs.Delete
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
NwBook.Activate
Dim Path As String
Dim filename As String
Path = "C:\Users\Gaurangg\Desktop\"
filename = Range("P1")
ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsb", FileFormat:=xlNormal
Range("J1").ClearContents
Range("A:A").Select
Selection.EntireColumn.Delete
Call MyFormat
Range("A2").Select
ActiveSheet.Protect Password:="Unique"
ThisWorkbook.Activate
Sheet2.Range("A1:N1").AutoFilter
Range("A:P").Select
Selection.EntireColumn.Delete
Range("A1").Select
Sheet1.Select
Range("A1").Select
Windows(NwBook).Close
Facing error while saving the file and then closing the file