Save as different file type - vba

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.

Related

File is not saving to newly made folder in VBA

I have a macro that created a folder by data within a pathway, and I want a cut of a manager roster to be saved in that folder. Since the folder name varies, this needs to be dynamic.
I want it to go something like this:
Dim sPath As String
sPath = "M:\mgr1_TCR_Reports\"
If Len(Dir(sPath & "_" & Format(Date, "mm_dd_yyyy"), vbDirectory)) = 0 Then
MkDir (sPath & "_" & Format(Date, "mm_dd_yyyy"))
End If
End Sub
and saving this like:
.SaveAs Filename:="M:\mgr1_TCR_Reports\" & "_" & Format(Date, "mm_dd_yyyy_") & "\" & Manager, FileFormat:=xlOpenXMLWorkbook, Password:=""
.Close
But I keep getting a runtime 1004: document not saved on ^^^ the second line of code I provided.
Any idea what's going on?

SaveAs to CSV in Excel for Mac 2016

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

Excel/VBA Remove text from ThieWorkbook.Name

I am trying to save a copy of an excel file through use of a marco but amend text after the current file name when saving. I have a macro that works, but it adds the file extension to the file name before I can amend text to it.
EG- my file is named "MyCurrentFile.xlsm", when I save it it adds the date, but keeps names the file "MyCurrentFile.xlsm01-14-16.xlsm".
Can I somehow remove the first .xlsm?
Code:
Sub Save_With_Todays_Date()
'
' Save_With_Todays_Date Macro
' Save a copy of the workbook with todays date at the end.
ThisWorkbook.SaveCopyAs _
Filename:=ThisWorkbook.Path & "\" & _
ThisWorkbook.Name & _
Format(Date, "mm-dd-yy") & ".xlsm"
End Sub
You can use the Workbook.FullName property and parse off the extension.
Dim fpfn as String
fpfn = ThisWorkbook.FullName
ThisWorkbook.SaveCopyAs _
Filename:=Left(fpfn, InStrRev(fpfn, Chr(46)) - 1) & Format(Date, "mm-dd-yy"), _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
I would recommend leaving the extension off the Workbook.SaveAs method and let the XlFileFormat Enumeration assign the correct extension. Hardcoding the extension reduces functionality and can result in an incorrect extension being applied to a SaveAs.
ThisWorkbook.Name = Replace(ThisWorkbook.Name, ".xlsm", Format(Date, "mm-dd-yy") & ".xlsm")

Excel VBA When using SaveAs Filename, strange things are happening to the file extension?

This is the line in my code:
ActiveWorkbook.SaveAs Filename:=curPath & cell.Value & Format(Now, "dmmmyyyy" & ".xlsx"), FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Here's the extension from a couple of the files:
.xl47x
.xl35x
I tried using ".xlsm" and I got:
.xl78
.xl22
I can force change the name and then the file will open - but why is it changing the extension? Does someone know what is going on? I've never seen this. Thank you!
Your file extension in the code needs to be outside the Format function. See the suggested approach below.
ActiveWorkbook.SaveAs Filename:=curPath & cell.Value & Format(Now, "dmmmyyyy") & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Issue with Saving Active Sheet to New workBook

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