Problem to SaveAs line

Hi,
I did get
21q.png
due to SaveAs line below. Why?
    Dim Time0 As Boolean, cnt0 As Integer, File0 As String, Title0 As String, Window0 As String, RowID0 As String, Var1 As String, Var2 As String, Var3 As String, Var4 As String, Var5 As String, Var6 As String, Var7 As String, Var8 As String, Var9 As String, Var10 As String, Var11 As String
    Time0 = True: cnt0 = 1: File0 = Application.ActiveWorkbook.Path & "\Departure List (of People already left).xls": Title0 = "Departure List (of People already left)": Window0 = "Departure List (of People already left).xls"
    
Rep0:   'Windows(Window1).Activate
    'Debug.Print Window1
    'Application.Workbooks(Window1).Activate
    With ThisWorkbook.Worksheets("Main Sheet").Range("AJ1:AJ50000")
        Set c = .Find(Search0, LookIn:=xlValues)
        If Not c Is Nothing Then
        
            Var1 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 3).Value
            Var2 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 4).Value
            Var3 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 5).Value
            Var4 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 6).Value
            Var5 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 7).Value
            Var6 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 8).Value)
            Var7 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 9).Value
            Var8 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 16).Value)
            Var9 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 17).Value)
            Var10 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 19).Value)
            Var11 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 20).Value
            
            RowID0 = CStr(c.Row) & "CC"
            'Windows(Window0).Activate
            Application.Workbooks(Window0).Activate
            If cnt0 = 1 Then
                Set HOBook = Workbooks.Add
                With HOBook
                    .Title = Title0
                    .Subject = Title0
                    .SaveAs Filename:=File0, FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled
                    .Close SaveChanges:=False
                End With

Open in new window

LVL 11
HuaMin ChenSystem AnalystAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
When the code fails, can you try saving it manually? If that fails, the message might be more useful.
0
 
Rgonzo1971Commented:
HI,

Is File0 consistent with FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled?

Regards
0
 
GrahamSkanRetiredCommented:
Have you tried
.SaveAs Filename:=File0, FileFormat:=xlOpenXMLWorkbookMacroEnabled

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
HuaMin ChenSystem AnalystAuthor Commented:
Sorry, I still get
21r.png
 due to SaveAs line below
    Dim Time0 As Boolean, cnt0 As Integer, File0 As String, Title0 As String, Window0 As String, RowID0 As String, Var1 As String, Var2 As String, Var3 As String, Var4 As String, Var5 As String, Var6 As String, Var7 As String, Var8 As String, Var9 As String, Var10 As String, Var11 As String
    Time0 = True: cnt0 = 1: File0 = Application.ActiveWorkbook.Path & "\Departure List (of People already left).xls": Title0 = "Departure List (of People already left)": Window0 = "Departure List (of People already left).xls"
    
Rep0:   'Windows(Window1).Activate
    'Debug.Print Window1
    'Application.Workbooks(Window1).Activate
    With ThisWorkbook.Worksheets("Main Sheet").Range("AJ1:AJ50000")
        Set c = .Find(Search0, LookIn:=xlValues)
        If Not c Is Nothing Then
        
            Var1 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 3).Value
            Var2 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 4).Value
            Var3 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 5).Value
            Var4 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 6).Value
            Var5 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 7).Value
            Var6 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 8).Value)
            Var7 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 9).Value
            Var8 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 16).Value)
            Var9 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 17).Value)
            Var10 = CStr(ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 19).Value)
            Var11 = ThisWorkbook.Worksheets("Main Sheet").Cells(c.Row, 20).Value
            
            RowID0 = CStr(c.Row) & "CC"
            'Windows(Window0).Activate
            Application.Workbooks(Window0).Activate
            If cnt0 = 1 Then
                Set HOBook = Workbooks.Add
                With HOBook
                    .Title = Title0
                    .Subject = Title0
                    '.SaveAs Filename:=File0, FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled
                    .SaveAs Filename:=File0, FileFormat:=xlOpenXMLWorkbookMacroEnabled
                    .Close SaveChanges:=False
                End With

Open in new window

0
 
GrahamSkanRetiredCommented:
I now don't think my previous comment was useful.

It might be a problem with the folder or the fact that the file is already open elsewhere.
0
 
ste5anSenior DeveloperCommented:
Clean up your code first, this makes testing and debugging simpler..

Option Explicit

Public Sub Test()

  CreateNewHOBook "C:\Temp\Text.xlsm", "TEST1"
 
End Sub

Public Sub CreateNewHOBook(AFileName As String, ATitle As String)

  Dim HOBook As Excel.Workbook
  
  Set HOBook = Workbooks.Add
  HOBook.Title = ATitle
  HOBook.Subject = ATitle
  HOBook.SaveAs Filename:=AFileName, FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled
  HOBook.Close SaveChanges:=False
  Set HOBook = Nothing
  
End Sub

Open in new window

0
 
HuaMin ChenSystem AnalystAuthor Commented:
Thanks to all.

Sorry, how to ensure that the same WorkBook would be close, after having created it like
                Set HOBook = Workbooks.Add
                With HOBook
                    .Title = Title0
                    .Subject = Title0
                    '.SaveAs Filename:=File0, FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled
                    .SaveAs Filename:=File0, FileFormat:=xlOpenXMLWorkbookMacroEnabled
                    .Close SaveChanges:=False
                End With

Open in new window

0
 
ste5anSenior DeveloperCommented:
Please rephrase your question. "the same WorkBook" makes non sense in this context.
0
 
HuaMin ChenSystem AnalystAuthor Commented:
I want to ensure that the created Workbook would be closed after having created it. How?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
After .SaveAs your open workbook is automatically renamed to the new name. You  then close it. There is no workbook open after that.
0
 
HuaMin ChenSystem AnalystAuthor Commented:
Can you share the codes to close it?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Your code in #a42487490 does that already!?
0
 
HuaMin ChenSystem AnalystAuthor Commented:
But I encounter
21t.png
due to SaveAs line below
            ...
            If cnt0 = 1 Then
                Set HOBook = Workbooks.Add
                With HOBook
                    .Title = Title0
                    .Subject = Title0
                    '.SaveAs Filename:=File0, FileFormat:=XlFileFormat.xlOpenXMLWorkbookMacroEnabled
                    .SaveAs Filename:=File0, FileFormat:=xlOpenXMLWorkbookMacroEnabled
                    .Close SaveChanges:=False
                End With

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Your last question here was "how to close", not "how to save", and I thought that part would have been solved?
File0 needs to contain the correct file extension for the type of workbook you want to store. You have used .xls, which does not work with current Excel releases and FileFormat:=xlOpenXMLWorkbookMacroEnabled. File0 should use .xlsm.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.