I'm experienced VBA developer but have no enough experience in Mac to complete my very simple project. I want to export sheets to CSV and save them into the same directory where workbook stored.
I already created a new workbook (ActiveWorkbook) with data copied from the main workbook and fully worked Windows part of code. But in Mac I cant make this part to work, no files created in result:
ThisWorkbook.Path & Application.PathSeparator & sheetname & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
I read few sources and found that Excel for Mac 2016 requires to call GetSaveAsFilename dialog with initial filename with manual confirmation to do it, so I modified my code like this:
#If Mac Then
s_fname = Application.GetSaveAsFilename(InitialFileName:= _
ThisWorkbook.Path & Application.PathSeparator & sheetname & ".csv")
If s_fname <> False Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs s_fname, 6
Application.DisplayAlerts = True
End If
#Else
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
ThisWorkbook.Path & Application.PathSeparator & sheetname & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
#End If
Macro creates files with data but they saved in XLSX not CSV:
and dialog have xlsx as default format:
When I'm trying to set FileFilter I have no files saved again (I assume Mac not supports this attribute). When I try to do replacement:
s_fname=replace(s_fname,".xslx",".csv")
I still have .xlsx files in the folder.
Can you suggest please which GetSaveAsFilename FileFilter value I must to set in the code to force CSV save instead of XLSX? Or anotehr VBA solution to do it (file format change, after-save renaming etc).
Thank you,
Viacheslav
Hi try to change first part as this one:
If Mac Then
OutputFile = ThisWorkbook.Path & Replace(ActiveWorkbook.Name, ".xls", "-") & ".csv"
If OutputFile <> False Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=OutputFile, FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
End If
Related
I have a simple code, which creates saves the actual file as csv file in another folder. How can I open this recently created file in notepad after the csv was created?
Here is the code, I tried with Call Shell but it didn't work.
Sub ConvertTocsv()
ChDir "S:\Back Office\Tradar\DailyReportBDP"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
("S:\Back Office\Tradar\DailyReportBDP\Custom_Daily_Report_BDP_" & Format(Now(), "YYYYMMDD") & ".csv"), FileFormat:=xlCSV, CreateBackup:=True, Local:=True
Application.DisplayAlerts = True
Information.Show
Call Shell("explorer.exe" & " " & "S:\Back Office\Tradar\DailyReportBDP", vbNormalFocus)
End Sub
Please provide some input. Thank you.
Solved it, here:
Sub ConvertTocsv()
Dim strfilename As String
strfilename = "S:\Back Office\Tradar\DailyReportBDP\Custom_Daily_Report_BDP_" & Format(Now(), "YYYYMMDD") & ".csv"
ChDir "S:\Back Office\Tradar\DailyReportBDP"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
("S:\Back Office\Tradar\DailyReportBDP\Custom_Daily_Report_BDP_" & Format(Now(), "YYYYMMDD") & ".csv"), FileFormat:=xlCSV, CreateBackup:=True, Local:=True
Application.DisplayAlerts = True
Information.Show
returnvalue = Shell("notepad.exe " & strfilename, vbNormalFocus)
ActiveWorkbook.Close SaveChanges:=False
End Sub
I am trying to save a copy of ThisWorkbook in xlsx while using SaveCopyAs. The problem is that SaveCopyAs saves the file as a macro enabled file, xlsb in my case which is not desired and while SaveAs does what I need, it also closes ThisWorkbook .
Is there an easier way to save in a format that does not support macros and without doing workarounds ?
Currently the best solution I found is the code snip below, but it's kinda finicky and it does not save the ContentTypeProperties that I need further on in the project, so does need to be added again.
Any ideas are appreciated, thanks!
Dim sPath as String
sPath = ThisWorkbook.FullName
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & sFileName & ".xlsx", 51
Application.Workbooks.Open (sPath)
Application.DisplayAlerts = True
Exit Sub
Define your path sPath = ThisWorkbook.Path & "\" & sFileName without extension
Use ThisWorkbook.SaveCopyAs sPath & ".xlsm" to save a copy.
Open that copy with Set Wb = Application.Workbooks.Open(sPath & ".xlsm")
Save again without Wb.SaveAs sPath & ".xlsx", xlOpenXMLWorkbook
Close workbook Wb.Close SaveChanges:=False
So something like the following should work, and would keep your current code running without any re-opening side effects of the current workbook.
Dim sPath As String
sPath = ThisWorkbook.Path & "\" & sFileName 'no extension!
ThisWorkbook.SaveCopyAs sPath & ".xlsm"
Dim Wb As Workbook
Set Wb = Application.Workbooks.Open(sPath & ".xlsm")
Wb.SaveAs sPath & ".xlsx", xlOpenXMLWorkbook
Wb.Close SaveChanges:=False
And probably you want to delete the xlsm file copy
Kill sPath & ".xlsm"
You might want to make use of Application.DisplayAlerts = False and Application.ScreenUpdating = False.
Hello, I'm trying to save a xlsm file as a CSV on a server I mapped on my computer. Here's the code:
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "Y:\"
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & "Project_Type_Test" & ".csv", FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
ThisWorkbook.Activate
VBA indicates that the bug comes form this line of code:
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & "Project_Type_Test" & ".csv", FileFormat:=xlCSV
I tried to switch ActiveWorkbook by ThisWorkbook, but it did not change anything.
Thanks in advance for your help!
I ran the code and it works fine for me if I set it to my hard drive.
Sub tst()Possibly try using the url instead of the mapped dive letter
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "C:\TEMP\" 'CHANGED THIS TO BE MY HD AND WORKS FINE
ActiveWorkbook.SaveAs Filename:=SaveToDirectory & "Project_Type_Test" & ".csv", FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
ThisWorkbook.Activate
End Sub
Possibly try using the url for the resource instead of the mapped drive letter
"\\machine\path\"
instead of "Y:\"
I would like to make my macro able to save a xlsx file as csv exactly with the same name when running it.
This is what I tried:
ActiveWorkbook.saveas Filename:=ActiveWorkbook.Path & "\" & _
ActiveWorkbook.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
However, it saves the file as .xlsx.csv (i.e, a file called prices.xlsx is saves as prices.xlsx.csv)
How could I save the file with a different file extension, without the .xlsx?
Filename := ActiveWorkbook.Path & "\" & Replace(ActiveWorkbook.Name,".xlsx", ".csv")
If you want to make it more failsafe, in case your extension may be xls, xlsm, xlsb, you can do something like this:
Dim parts As Variant
parts = Split(ActiveWorkbook.Name, ".")
parts(UBound(parts)) = "csv"
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & _
Join(parts, "."), FileFormat:=xlCSV, CreateBackup:=False
It's probably not 100% bulletproof, although I'm struggling to think of a situation where it would not work as expected.
I am trying to save an Active sheet in Excel using following VBA:
Application.CutCopyMode = False
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xls"
ActiveWorkbook.SaveAs Filename:=FName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
Activeworkbook.close
but I am having two issues here:
1- When I want to open the file I am encountring with following Message:
Manually opening the file is OK by pressing the Yes but I am going to use the Excel File in GIS software which causing problem because of misunderstanding of format. As you can see it has .xls format
2- the Activeworkbook.close is not functioning since I have to close the Application after running the code by my own!
The first part is very important for me, to understand why this is happening? can you please let me know why?
You are using the wrong file format.
For .xls it is xlExcel8. xlOpenXMLWorkbookMacroEnabled is for .xlsm
Either use this
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & _
Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xls"
ActiveWorkbook.SaveAs Filename:=FName, _
FileFormat:=xlExcel8
or use this
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & _
Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=FName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
Regarding your 2nd question. Change your code to this
Application.DisplayAlerts = False
FName = "C:\Users\Public\Documents\DTMForGIS\DTMtoGIS" & _
Format(Now, "yyyy-mm-dd hh_mm_ss") & ".xls"
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:=xlExcel8
With ActiveSheet.UsedRange
.Copy
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
Application.Quit