VBA code help need to save workbook with name as cell value

Gaurangg

Board Regular
Joined
Aug 6, 2015
Messages
134
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

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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
filename = Range("P1")
You need to tell it which workbook and which sheet. ie. filename = Thisworkbook.Sheets(1).Range("P1").
Otherwise it does not know what you want to do and will ignore the line of code leaving your variable empty. So the SaveAs will not work with the empty variable.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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