Error trying to save xltm as xlsx

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,418
Office Version
  1. 2016
Platform
  1. Windows
Hi guys - I'm encountering an error when I try to save a template as a .xlsx file.

The error is object variable or with block variable not set and it appears on line 40 of this code:

VBA Code:
Private Sub Workbook_Open()
10    If Sheets("SUMMARY").Range("D2").Value = "" Then
20    AnswerYes = MsgBox("Import Data?", vbQuestion + vbYesNo, "Import")
30    If AnswerYes = vbYes Then
40    ImportFrm.Show
50    End If
60    End If
End Sub

The save routine is here:

VBA Code:
Sub SaveFile()

Application.DisplayAlerts = False
Application.ScreenUpdating = False

If InStr(Sheets("SUMMARY").Range("D2").Value, "2023") Then ImportYear = "2023"
If InStr(Sheets("SUMMARY").Range("D2").Value, "2024") Then ImportYear = "2024"

Filename = Sheets("SUMMARY").Range("D2").Value & " Times Data"
FilePath = Drive & "Times\Completed\" & ImportYear

ThisWorkbook.SaveAs Filename:=FilePath & "\" & Filename & ".xlsx", FileFormat:=51

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

The file saves fine, as expected, but I get that error afterwards and I don't understand why - can anyone clear up my confusion?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I tried to replicate your problem, but could not. Though I did discover a few issues in your code. Try this version to see if it makes any difference.
VBA Code:
Sub SaveFile()
    Dim ImportYear As String, Filename As String, Drive As String, FilePath As String, FullName As String, Msg As String
    
    Msg = "Performing a 'Save-As' to a new file will abandon any unsaved changed in this workbook ('" & ThisWorkbook.Name & "')" & vbCr & vbCr
    Msg = Msg & "Do you wish to save any changes to '" & ThisWorkbook.Name & "' before proceeding with the 'Save-As' operation?"
    
    Select Case MsgBox(Msg, vbYesNoCancel Or vbQuestion, Application.Name)
        Case vbYes
            ThisWorkbook.Save
        Case vbCancel
            Exit Sub
    End Select
        
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    If InStr(Sheets("SUMMARY").Range("D2").Value, "2023") Then
        ImportYear = "2023"
    ElseIf InStr(Sheets("SUMMARY").Range("D2").Value, "2024") Then
        ImportYear = "2024"
    Else
        MsgBox "'" & Sheets("SUMMARY").Range("D2").Value & _
            "' is not a valid year.", vbCritical, "Data Error"
        Exit Sub
    End If
    
    Drive = "C:\"   '<-- edit as required
    Filename = Sheets("SUMMARY").Range("D2").Value & " Times Data"
    FilePath = Drive & "Times\Completed\" & ImportYear
    FilePath = "C:\Users\223103252\Documents\TestFiles"
    
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(FilePath) Then
            MsgBox "Folder '" & FilePath & "' does not exist.", vbCritical, _
                "Folder Error"
            Exit Sub
        End If
    End With
    
    FullName = FilePath & "\" & Filename & ".xlsx"
    
    ThisWorkbook.SaveAs Filename:=FullName, FileFormat:=51
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
An alternate approach to the above.
VBA Code:
'Save a copy of current Workbook a new file format while keeping the original workbook open.
Sub SaveFileV2()
    Dim ImportYear As String, Filename As String, Drive As String, FilePath As String, FullName As String, TempFilePath As String
    Dim DestWB As Workbook
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    If InStr(Sheets("SUMMARY").Range("D2").Value, "2023") Then
        ImportYear = "2023"
    ElseIf InStr(Sheets("SUMMARY").Range("D2").Value, "2024") Then
        ImportYear = "2024"
    Else
        MsgBox "'" & Sheets("SUMMARY").Range("D2").Value & _
        "' is not a valid year.", vbCritical, "Data Error"
        Exit Sub
    End If
    
    Drive = "C:\"
    Filename = Sheets("SUMMARY").Range("D2").Value & " Times Data"
    FilePath = Drive & "Times\Completed\" & ImportYear
    FilePath = "C:\Users\223103252\Documents\TestFiles"
    
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(FilePath) Then
            MsgBox "Folder '" & FilePath & "' does not exist.", vbCritical, _
            "Folder Error"
            Exit Sub
        End If
        TempFilePath = FilePath & "\TmpFile$." & .GetExtensionName(ThisWorkbook.Name)
    End With
    
    ThisWorkbook.SaveCopyAs (TempFilePath)
    DoEvents
    Set DestWB = Application.Workbooks.Open(Filename:=TempFilePath)
    DoEvents
    
    FullName = FilePath & "\" & Filename & ".xlsx"
    
    DestWB.SaveAs Filename:=FullName, FileFormat:=51
    DestWB.Close False
    DoEvents
    Kill TempFilePath
    
    MsgBox "New File Created:" & vbCr & vbCr & FullName, vbInformation
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
An alternate approach to the above.
VBA Code:
'Save a copy of current Workbook a new file format while keeping the original workbook open.
Sub SaveFileV2()
    Dim ImportYear As String, Filename As String, Drive As String, FilePath As String, FullName As String, TempFilePath As String
    Dim DestWB As Workbook
   
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
   
    If InStr(Sheets("SUMMARY").Range("D2").Value, "2023") Then
        ImportYear = "2023"
    ElseIf InStr(Sheets("SUMMARY").Range("D2").Value, "2024") Then
        ImportYear = "2024"
    Else
        MsgBox "'" & Sheets("SUMMARY").Range("D2").Value & _
        "' is not a valid year.", vbCritical, "Data Error"
        Exit Sub
    End If
   
    Drive = "C:\"
    Filename = Sheets("SUMMARY").Range("D2").Value & " Times Data"
    FilePath = Drive & "Times\Completed\" & ImportYear
    FilePath = "C:\Users\223103252\Documents\TestFiles"
   
    With CreateObject("Scripting.FileSystemObject")
        If Not .FolderExists(FilePath) Then
            MsgBox "Folder '" & FilePath & "' does not exist.", vbCritical, _
            "Folder Error"
            Exit Sub
        End If
        TempFilePath = FilePath & "\TmpFile$." & .GetExtensionName(ThisWorkbook.Name)
    End With
   
    ThisWorkbook.SaveCopyAs (TempFilePath)
    DoEvents
    Set DestWB = Application.Workbooks.Open(Filename:=TempFilePath)
    DoEvents
   
    FullName = FilePath & "\" & Filename & ".xlsx"
   
    DestWB.SaveAs Filename:=FullName, FileFormat:=51
    DestWB.Close False
    DoEvents
    Kill TempFilePath
   
    MsgBox "New File Created:" & vbCr & vbCr & FullName, vbInformation
   
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
Thanks very much, I'll give it a try
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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