Save a file

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this code it creates a folder and code is trying to save the current file in the created folder

but not sure its not working, can there be a fix.



VBA Code:
Sub FoldtoBackup()

'===================================================
    ' This will creat a backup folder by name Daily Hajj Final Report
'===================================================

Dim Path As String
Dim path1 As String
Dim Folder As String
Dim Answer As VbMsgBoxResult
Dim relativePath As String
    Path = Application.ThisWorkbook.Path & "\" & "Daily Hajj Final Report"
    Folder = Dir(Path, vbDirectory)
    If Folder = vbNullString Then
                 VBA.FileSystem.MkDir (Path)
    'Exit Sub
    Else
        'MsgBox "Folder exists."
    End If
 
 
Sheets(Array("Summary", "Sheet1", "Fiber RawData", _
        "Copper RawData", "FTTM 1 RawData", "FTTM 2 RawData", "FTTH RawData", "Fiber Mak Mad Haram")).Select
    Sheets("Summary").Activate
    Sheets(Array("Summary", "Sheet1", "Fiber RawData", _
        "Copper RawData", "FTTM 1 RawData", "FTTM 2 RawData", "FTTH RawData", "Fiber Mak Mad Haram")).Copy
    ChDir Path           '"D:\"
    
    On Error Resume Next
    'Kill "D:\Print Raw Data.xlsx"
    ActiveWorkbook.SaveAs Filename:= _
        Path & "\" & Sheets("Report").Range("d5").Value.xlsx, FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close  ''' Here is the problem
    Sheets("Report").Select
    Range("E7").Select
 
 
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I have this code it creates a folder and code is trying to save the current file in the created folder

but not sure its not working, can there be a fix.



VBA Code:
Sub FoldtoBackup()

'===================================================
    ' This will creat a backup folder by name Daily Hajj Final Report
'===================================================

Dim Path As String
Dim path1 As String
Dim Folder As String
Dim Answer As VbMsgBoxResult
Dim relativePath As String
    Path = Application.ThisWorkbook.Path & "\" & "Daily Hajj Final Report"
    Folder = Dir(Path, vbDirectory)
    If Folder = vbNullString Then
                 VBA.FileSystem.MkDir (Path)
    'Exit Sub
    Else
        'MsgBox "Folder exists."
    End If
 
 
Sheets(Array("Summary", "Sheet1", "Fiber RawData", _
        "Copper RawData", "FTTM 1 RawData", "FTTM 2 RawData", "FTTH RawData", "Fiber Mak Mad Haram")).Select
    Sheets("Summary").Activate
    Sheets(Array("Summary", "Sheet1", "Fiber RawData", _
        "Copper RawData", "FTTM 1 RawData", "FTTM 2 RawData", "FTTH RawData", "Fiber Mak Mad Haram")).Copy
    ChDir Path           '"D:\"
   
    On Error Resume Next
    'Kill "D:\Print Raw Data.xlsx"
    ActiveWorkbook.SaveAs Filename:= _
        Path & "\" & Sheets("Report").Range("d5").Value.xlsx, FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close  ''' Here is the problem
    Sheets("Report").Select
    Range("E7").Select
 
 
End Sub
Are you trying to save all of the sheets in the workbook?
 
Upvote 0
This:
VBA Code:
    ActiveWorkbook.SaveAs Filename:= _
        Path & "\" & Sheets("Report").Range("d5").Value.xlsx, FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False

probably needs to be this:
VBA Code:
    ActiveWorkbook.SaveAs Filename:= _
        Path & "\" & Sheets("Report").Range("d5").Value & ".xlsx", FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False

Note: If you had not turned off error checking with this statement: On Error Resume Next, it would probably be a lot more obvious. Error checking is your friend.
 
Upvote 0
Which of these lines is causing the error?

VBA Code:
ActiveWindow.Close  ''' Here is the problem
    Sheets("Report").Select
    Range("E7").Select
 
Upvote 0
Path & "\" & Sheets("Report").Range("d5").Value & ".xlsx"
It is giving me this error as Run-time error '-214735....'
The specified dimension is not valid for the current chart type.

VBA Code:
 'On Error Resume Next
    
    ActiveWorkbook.SaveAs Filename:= _
        Path & "\" & Sheets("Report").Range("d5").Value & ".xlsx", FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False
 
Upvote 0
Which of these lines is causing the error?

VBA Code:
ActiveWindow.Close  ''' Here is the problem
    Sheets("Report").Select
    Range("E7").Select

This part

VBA Code:
 'On Error Resume Next
    
    ActiveWorkbook.SaveAs Filename:= _
        Path & "\" & Sheets("Report").Range("d5").Value & ".xlsx", FileFormat _
        :=xlOpenXMLWorkbook, CreateBackup:=False
 
Upvote 0
The Path property in Excel VBA returns the complete, saved path to a workbook.

Do not use Path as a variable name although this was not the problem.

Try this.

VBA Code:
Sub FoldtoBackup()
Dim strPath As String
Dim path1 As String
Dim Folder As String
Dim Answer As VbMsgBoxResult
Dim relativePath As String
Dim strFileName As String

    strPath = Application.ThisWorkbook.Path & "\" & "Daily Hajj Final Report"
    
    Folder = Dir(strPath, vbDirectory)
    
    If Folder = vbNullString Then
        VBA.FileSystem.MkDir (strPath)
    Else
        MsgBox "Folder exists."
    End If

    strFileName = strPath & "\" & Sheets("Report").Range("D5").Value & ".xlsx"
    
    Worksheets(Array("Summary", "Sheet1", "Fiber RawData", _
        "Copper RawData", "FTTM 1 RawData", "FTTM 2 RawData", "FTTH RawData", "Fiber Mak Mad Haram")).Copy
    
    With ActiveWorkbook
        .SaveAs Filename:=strFileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        .Close
    End With
    
    With ActiveWorkbook
        .Worksheets("Report").Activate
        ActiveSheet.Range("E7").Select
    End With
      
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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