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
Related
Im trying to save a file into a location which will then be used to run a report from.
Currently this code is saving the file as a macro enabled worksheet, I need it to be just a .xlsx
Sub SaveToDesktop()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With ActiveWorkbook
.SaveAs "W:\Logistics\Distribution\Chep\Chep Dehire Master Control\In" & "\" & Format(Now, "hh-nn-ss-dd-mm-yyyy") & " " & ActiveWorkbook.Name
'.Close
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Thanks, file saved."
Application.Quit
End Sub
Any help or ideas would be greatly appreciated.
Thanks guys, problem solved!
Read through the guide and worked from karma's suggestion.
Sub Saveworkbook()
Dim Path As String
Dim Filename As String
Path = "W:\Logistics\Distribution\Chep\Chep Dehire Master Control\In\"
Filename = ActiveWorkbook.Name & Format(Now, "hh-nn-ss dd-mm-yyyy")
Application.DisplayAlerts = False
Application.ScreenUpdating = False
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xlsx", FileFormat:=51
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Thanks, file saved."
Application.Quit
End Sub
I am using the following code to create new sheets based on an bigger excell file.
I would like to rename the files like "nameofthesheet_Vouchering Hours August 2022_YTD" for each sheet, currently I am only getting "nameofthesheet". How can I change the code in order to have it like this?
Sub Separar_guias()
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
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!
Hello and hi everyone.
I'm sorry if what I asked right now sounds dumb or stupid, but I'm relatively new to VBA's and not really fluent with it.
Now, I have a form for user to input report on incident happened at their workplace and I manage to save the Data worksheet as csv but now, I want to automatically save the sheet as username and time the csv printed.
Here is what my code look like.
Public Sub ExportWorksheetAndSaveAsCSV()
Dim wbkExport As Workbook
Dim shtToExport As Worksheet
Dim xStrDate As String
Set shtToExport = ThisWorkbook.Worksheets("PartsData")
xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
Application.DisplayAlerts = False
wbkExport.SaveAs FileName:=xStrDate,"C:\temp\IncidentDatabase.csv", FileFormat:=xlCSV
Application.DisplayAlerts = True
wbkExport.Close SaveChanges:=False
End Sub
and when I try to run the code, this happened
it's highlighted on this part -
wbkExport.SaveAs FileName:=xStrDate,"C:\temp\IncidentDatabase.csv", FileFormat:=xlCSV
I have read some of almost similar question but found no answer. Please help me.
Thank you.
Regard.
Copy the worksheet without providing a location. This creates a new workbook with the copied worksheet as the only worksheet.
The ENVIRON function can retrieve environment variables like the user name.
Public Sub ExportWorksheetAndSaveAsCSV()
Dim fn As String
fn = environ("USERNAME") & "-" & Format(Now, "yyyy-mm-dd hh-mm-ss")
ThisWorkbook.Worksheets("PartsData").Copy
With activeworkbook
Application.DisplayAlerts = false
.SaveAs FileName:="C:\temp\" & fn, FileFormat:=xlCSV
Application.DisplayAlerts = true
.close savechanges:=false
end with
End Sub
I think code line
wbkExport.SaveAs FileName:=xStrDate,"C:\temp\IncidentDatabase.csv", FileFormat:=xlCSV
should be changed to
wbkExport.SaveAs FileName:="C:\temp\IncidentDatabase - " & xStrDate & ".csv", FileFormat:=xlCSV
for proper working
Please see .SaveAs properties here.
The first argument is the name & the second is the file type.
Dim loc As String
loc = "C:\temp\"
xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
wbkExport.SaveAs loc & wbkExport.Name & xStrDate, xlCSV
I found code to save a single sheet as CSV.
But I am getting error:
Run-time error '1004'
Cannot access read-only document 'MasterCallOneList.CSV'
How to fix?
The document is actually a new document that does not exist, so I don't know why it would say that the document is read-only.
Application.DisplayAlerts = False
Dim strFullName As String
strFullName = Application.Path + "\MasterOneCallList.CSV"
ThisWorkbook.Sheets("Combined").Copy
ActiveWorkbook.SaveAs Filename:=strFullName, FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close
Application.DisplayAlerts = True
Could be due to protection levels in the application.path directory.
Have you tried
Application.DisplayAlerts = False
Dim strFullName As String
strFullName = ThisWorkbook.Path + "\MasterOneCallList.CSV"
ThisWorkbook.Sheets("Combined").Copy
ActiveWorkbook.SaveAs Filename:=strFullName, FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close
Application.DisplayAlerts = True
strFullName = Application.Path + "\MasterOneCallList.CSV"
This could be the error, the path name would be the ms_office excel folder. Try using an actual folder path string and the code should work. If it does work, you would have to figure a way to get an actual path string.
Sub Button1_Click()
Dim strFullName As String
Application.DisplayAlerts = False
strFullName = "C:\Users\dmorrison\Downloads\TestKryztof" + "\MasterOneCallList.CSV"
ThisWorkbook.Sheets("Combined").Copy
ActiveWorkbook.SaveAs Filename:=strFullName, FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub