Subscript Out of Range Error When Closing Workbook - excel

I'm currently trying to edit a macro that opens a base file [myFile], runs a separate macro in that base file. By running this base file, an entirely new workbook is created [myFile2]. The goal is to take myFile2, do some resizing, move some sheets around, etc., and create a differently formatted report [myFile3].
Everything is working fine until I go to close the workbooks, where I'm getting a subscript out of range error.
This wasn't my original code. I'm now editing it, and I don't have access to the original author. The word 'FALSE' is by each of the workbook close statements, and I'm also wondering if that is related to save changes or something else that could be contributing to the problem?
myPath = "\this\is\a\file\path"
myFile = "base.xlsm"
myPath2 = "\this\is\another\path"
myFile2 = Format(Date, "yymmdd") & "-base_results" & ".xlsm"
myPath3 = "\this\is\a\third\path"
myFile3 = "formatted_base_results" & Format(Date, "mm-dd-yyyy") & ".xlsm"
'open workbook to run macros
Set wkb = Workbooks.Open(Filename:=myPath2 & "\" & myFile2, UpdateLinks:=False)
'run macros in opened workbook
Application.Run ("'" & wkb.Name & "'!" & "MoveSheets")
DoEvents
Application.Run ("'" & wkb.Name & "'!" & "FormatSheets")
DoEvents
Application.Run ("'" & wkb.Name & "'!" & "NewDocument")
DoEvents
'close workbooks
'cant index by full file name
Workbooks(myFile).Close False
Workbooks("DailyBuildPrint - Avg Fare" & Format(Date, "mm-dd-yyyy") & ".xlsm").Close False
Workbooks(myFile2).Close False

Related

How can I add the date to the excel file title with vba code?

Good morning,
Currently I this code below to create from sheet 5 some Excel files with same name at sheet. However I would like to add current date as: "List AA 30.03.2022".
Sub EXCELS()
'Create excel files
Dim i As Integer
Dim name_file As String
For i = 5 To Sheets.Count
name_file = Sheets(i).Name
Worksheets(i).Copy
With ActiveWorkbook
.SaveAs Filename:=ThisWorkbook.Path & "\" & name_file & ".xlsx",
FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
Next i
End Sub
What do I need to add?
Try this :
ThisWorkbook.Path & "\" & name_file & " " & Format(Date, "DD.MM.YYYY") & ".xlsx"
Replace the line .SaveAs Filename:=ThisWorkbook.Path & "\" & name_file & ".xlsx", FileFormat:=xlOpenXMLWorkbook with
.SaveAs Filename:=ThisWorkbook.Path & "\" & name_file & Format(Date, " dd.mm.yyyy") & ".xlsx", FileFormat:=xlOpenXMLWorkbook
EDIT
per your comment, in order to replace the file completely, you'll first need to save the old file name as a variable, then delete it afterwards.
So, replace the entire With block with the below;
With ActiveWorkbook
'variable to store the old file name:
Dim OldFileName as String
'assign the file's current name to the variable:
OldFileName = .FullName
'Now save the file with it's new name, then close it:
.SaveAs _
Filename:=ThisWorkbook.Path & "\" & name_file & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook
.Close
End With
'Get rid of the leftover file with the old name:
Kill OldFileName

I want to make a save as with file name as, time stamp(space)text in range M2(space)Design Format

I want to make a save as with file name as, time stamp(space)text in range M2(space)Design Format
example: 20200525 1747 Client Design Format
Range M2 = Client
Sub Make_saveas()
Worksheets(Array("Sheet1", "Sheet2")).Copy
With ActiveWorkbook
.SaveAs Filename:=Environ("USERPROFILE") & "\Desktop\" & Format(Now(),
"YYYYMMDD HHMM, sheets(3).range("M2"), "Design Format"")& ".xlsx",
FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End With
End Sub
Please help me with the above code.
Try the following...
Filename:=Environ("userprofile") & "\Desktop\" & Format(Now(),"YYYYMMDD HHMM") & " " & sheets(3).range("M2") & " Design Format.xlsx"

Errors when using Mac Excel macro to export CSV

Mac Excel v16.16.7 - Trying to use a macro to export the active sheet of an xlsm file to a csv with the sheet name as the file name.
I'm aware of the sandboxing issues and thought I was getting around them with GrantAccessToMultipleFiles, but the exact macro below delivers three different outcomes:
Sometimes it works. Saves the sheet as a CSV in the same directory as the workbook.
400 error
Run-time error '1004' Application-defined or object-defined error
Sub SaveAsCSV()
Dim strName As String
Dim fileAccessGranted As Boolean
Dim filePermissionCandidates
filePermissionCandidates = Array(ThisWorkbook.Path & Application.PathSeparator & ActiveSheet.Name & ".csv")
fileAccessGranted = GrantAccessToMultipleFiles(filePermissionCandidates)
If fileAccessGranted = True Then
Application.ScreenUpdating = False
strName = ThisWorkbook.Path & Application.PathSeparator & ActiveSheet.Name & ".csv"
ActiveSheet.Copy 'copy the sheet as a new workbook
ActiveWorkbook.SaveAs Filename:=strName, FileFormat:=xlCSV
ActiveWorkbook.Close SaveChanges:=False
Application.ScreenUpdating = True
MsgBox "File has been Created and Saved as: " & vbCr & strName, , "Copy & Save Report"
End If
End Sub
I'm hoping for some insight into why the outcomes are varied. Thanks!

Copy-save a sheet in a new workbook named after contents of some specific cells from that same sheet

I have a sheet in a workbook (.xlsm) I want to "copy" in a new workbook (.xlsx). In the process, I also need the new workbook to be named after the cells B3 and B5 from the same sheet.
I found codes here and there either to create a new workbook from a sheet, either to rename an existing workbook. I tried to 'mix' it, but I'm stuck with the "Filename:=Name" argument. It returns an error. My only way to name the new workbook is to use "". If someone knows the reason and has any guidance for a fix, it would be appreciated.
Sub Copy_Save_Sheet_As_Workbook()
Dim wb As Workbook
Dim Path As String
Dim Name As String
Set wb = Workbooks.Add
Path = "C:\Users\..." 'That line is not used now. Will try later to work on it.
Name = Worksheets("Sheet1").Range("B5")
ThisWorkbook.Activate
Worksheets("Sheet1").Copy Before:=wb.Sheets(1)
wb.Activate
wb.SaveAs FileName:=Name & ".xlsx", FileFormat:=xlNormal
End Sub
== EDIT ==
Here is the modified code after the fix from CATSandCATSandCATS.
Sub Copy_Save_Sheet_As_Workbook()
Dim wb As Workbook
Set wb = Workbooks.Add
ThisWorkbook.Activate
Worksheets("Sheet1").Copy Before:=wb.Sheets(1)
wb.Activate
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Range("B3") & Range("B5") & ".xlsx"
End Sub
Try this:
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Range("B3") & Range("B5") & ".xlsx"
in place of this:
wb.SaveAs FileName:=Name & ".xlsx", FileFormat:=xlNormal
And obviously you can add more things to the file, such as a date:
& Format(Now, "mm.dd.yyyy")
Or text:
& "1Q2019 - March Budget"
And if you do not want to save it in the same location as the file containing the macro, you can just add your "C:\Users..." in place of ThisWorkbook.path.

Copy tabs in to a new unopened file and send in email with text in body and subject - VBA - Macro

have a segment of code that attaches copy tabs from a document and pastes them into a new document. Then sends only the new document to the intended addressees.
Is there a way I can change the name of the file I'm intending to send? It just sends as Book1.
Additionally, I'd like to add text in the body and the subject header of the email too. How can I go about doing this?
Sub Sendtabonemail()
Dim wb As Workbook
Dim strbody As String
Set wb = Workbooks.Add
ThisWorkbook.Sheets("sheet6").Copy After:=wb.Sheets(1)
ThisWorkbook.Sheets("sheet3").Copy After:=wb.Sheets(1)
wb.Application.Dialogs(xlDialogSendMail).Show "" & "emailreceiver1#domain.com" & "; " & "emailreceiver2#domain.com"
End Sub
Sub dfjero()
Dim newWBname As String
newWBname = Sheets(1).Name & "_" & Month(Date) & "_" & Day(Date) & "_" & Year(Date)
Workbooks.Add
If Len(Dir("c:\newFile", vbDirectory)) = 0 Then ' create and delete temporary directory and file
MkDir "c:\newFile"
ActiveWorkbook.SaveAs Filename:="C:\newFile\" & newWBname & ".xls", FileFormat:=xlExcel8
' This is where you send the book via email
ActiveWorkbook.Close
On Error Resume Next
Kill "C:\newFile\" & newWBname & ".xls"
RmDir "C:\newFile\"
On Error GoTo 0
Else ' or add file to already created directory
ActiveWorkbook.SaveAs Filename:="C:\newFile\" & newWBname & ".xls", FileFormat:=xlExcel8
' or alternatively this is where you send the workbook via email
End If
End Sub
It's calling it Book1 because that's the default name for a new workbook until it's saved. To name it, just save the file to a temporary location (with a name of your choosing) before sending.
wb.SaveAs "C:\temporary folder location\filename_to_use.xlsx"

Resources