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!
Related
My company has had a Macro that has been running without change for the past two years. With the new Microsoft updates the code has been failing. The Macro has been designed to upload data from another document, re-arrange it, and then send it to a Sample Manager. The following code creates a run-time error '1004' Copy method of Worksheet class failed. When I hit debug ActiveSheet.Copy is highlighted. If anyone has any suggestions, it would help. The code is:
Sub ExportToLims()
Dim fname As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
fname = "X:\LIMS Integration Manager\Entech7200\Input\" & ActiveSheet.Name & "-" & Format(Now(), "mmddyy-hhmm") & ".csv"
ActiveSheet.Copy
ActiveWorkbook.SaveAs filename:=fname, FileFormat:=xlCSV
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox ("Injection Report Exported to LIMS IM")
End Sub
I am trying to use the code below to save Workbook files that are created from individual sheets in a secondary workbook into my department's SharePoint, but the files are not showing up after the macro has run. There are no errors showing up and when I tried running this macro in a folder on my desktop it saved all the files correctly. Any ideas as to why it just won't work when its in a SharePoint?
Sub SaveShtsAsBook()
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
Dim theFilePath As String
MyFilePath$ = ActiveWorkbook.Path & "\"
For Each Sheet In ThisWorkbook.Worksheets
SheetName$ = Sheet.Name
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' End With
On Error Resume Next '<< a folder exists
theFilePath = MyFilePath & "FY 20-21 P Card Statements - " & SheetName
MkDir theFilePath
theFilePath = theFilePath & "/" & "February Statement 1.6.21-2.6.21"
MkDir theFilePath
With Sheet
.Select
.Copy
ActiveWorkbook.SaveAs Filename:=theFilePath & "/" & SheetName & " Feb-06.xlsx", FileFormat _
:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
End With
.CutCopyMode = False
End With
Next Sheet
End Sub
I'm trying to make a button that saves my workbook with the name of a specific cell from one of its own sheets.
The cell that I'm getting the name from is gonna change it's value each week.
I get the error:
Run-time error '1004':
Method 'SaveAs' of object'_Workbook' failed
Here is the code:
Private Sub Save_file_Click()
Dim path As String
Dim filename1 As String
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Fejlregistrering")
path = "C:\Users\JOHLA\Desktop\Yield ark\"
filename1 = ws.Range("D5").Text
ActiveWorkbook.SaveAs path & filename1 & ".xlsm", FileFormat = xlOpenXMLWorkbookMacroEnabled
End Sub
//Johan
Change
ActiveWorkbook.SaveAs path & filename1 & ".xlsm", FileFormat = xlOpenXMLWorkbookMacroEnabled
to
ActiveWorkbook.SaveAs Filename:=(path & filename1 & ".xlsm"), FileFormat:=xlOpenXMLWorkbookMacroEnabled
try with below
ActiveWorkbook.SaveAs Filename:=path & filename1, FileFormat:=xlOpenXMLWorkbookMacroEnabled
I am encountering an error on the code that I am working on below.
Sub Pasting
Dim o as integer
Dim i as integer
Dim v as String
o = 1
i = 0
Sheets("Sample").Visible = True
Sheets("Sample").Select
Do While i < 1
Range("A:AA").Select
Selection.Copy
ActiveSheet.Next.Select
On Error Goto PE
Range("A1").Select
Application.DisplayAlerts = False
ActiveSheet.Paste
Application.DisplayAlerts = True
Loop
PE:
Application.CutCopyMode = False
Sheets("Sample").Visible = False
Sheeets("Overall").Select
v = "Sample File" & Format(DateAdd("m",1,Now), "Mmmm yyyy") & ".xlsb"
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & v
End Sub
My Error was a
Run-Time Error 1004
Method 'SaveAs' of object'_Workbook Failed.
Process would be:
You will open the Previous File from the previous month
Click file to Open Sample sheet w/c contains default table
Run the loop until all all sheets from 1 to 30 has been paste with default data
Macro will end loop
Macro will save the file as same file type from the same location with the new month.
Close file and override error messages.
At least three things
You should use a workbook name/variable rather than ActiveWorkbook to avoid accidentally using the wrong workbook. I can't see from your code where ActiveWorkbook comes from. One danger could be that it is unintentionally ThisWorkbook.
If ThisWorkbook isn't already saved then the ThisWorkbook.Path will be "" and you will get an error
You should specify the file format for xlsb on save (though if already an xlsb there won't be an error)
VBA:
Dim v As String
v = "Sample File" & Format(DateAdd("m", 1, Now), "Mmmm yyyy") & ".xlsb"
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & v, FileFormat:=50
I have an Excel macro that is copying all of the information from a specific worksheet and copying it into a new workbook. The code is as follows:
Option Explicit
Sub TwoSheetsAndYourOut()
Dim NewName As String
Dim nm As Name
Dim ws As Worksheet
If MsgBox("Copy specific sheets to a new workbook" & vbCr & _
"New sheets will be pasted as values, named ranges removed" _
, vbYesNo, "New Copy") = vbNo Then Exit Sub
With Application
.ScreenUpdating = False
On Error GoTo ErrCatcher
Sheets("Input").Copy
On Error GoTo 0
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Copy
ws.[A1].PasteSpecial Paste:=xlValues
ws.Cells.Hyperlinks.Delete
Application.CutCopyMode = False
Cells(1, 1).Select
ws.Activate
Next ws
Cells(1, 1).Select
For Each nm In ActiveWorkbook.Names
nm.Delete
Next nm
NewName = InputBox("Please specify the name of your new workbook", "New Copy", "input")
Dim sPath As String
sPath = ThisWorkbook.Path
ActiveWorkbook.SaveCopyAs sPath & NewName + ".xls"
ActiveWorkbook.Close SaveChanges:=False
.ScreenUpdating = True
End With
Exit Sub
ErrCatcher:
MsgBox "Specified sheets do not exist within this workbook"
End Sub
However, it does not save the new Excel file in the correct directory. The original Excel file, the one that contains the macro, is in the following directory (on a Mac):
/Applications/WORDNET/PROJECTS
However, every time I run the macro, it saves the new Excel file in the WORDNET folder, instead of the PROJECTS folder.
How do I modify the code so that it saves in the correct place? And why does it not save in the same directory as the original Excel file?
sPath = ThisWorkbook.Path
sPath is the path without a seperator at the end (at least on Windows) so you have to add one in your script. In your case, the files will be saved to /Applications/WORDNET with the name "PROJECTS" & NewName
Unix:
ActiveWorkbook.SaveCopyAs sPath & "/" & NewName + ".xls"
Windows:
ActiveWorkbook.SaveCopyAs sPath & "\" & NewName + ".xls"