I am trying to find a fast way to save my xlsx files as csv files with the same filename as the xlsx file (just in csv format). I have recorded a macro with shortcut, but the issue is that whenever I try with a new file it saves as a the same filename I recorded initial macro with (i.e. see below, probably because I have the file labelled in code as: 3WDL_1 (2014-08-07)10secDataTable sit.csv). Is there something I need to replace 3WDL_1 (2014-08-07)10secDataTable sit.csv with to make the macro save with the same filename as the actual workbook I am working with.
So basically I have a folder full of xlsx files and I want to use a shortcut/macro on each xslx file to convert them into a csv files that have exactly the same name as original xlsx file, and are saved into the same folder.
I need to replace the 3WDL_1 (2014-08-07)10secDataTable sit.csv portion of code with something that will work with any new xlsx file I open up. Otherwise I may as well just do it the old fashioned way with mouse
Thanks so much in adcance,
Paddy
Sub SaveAsCSVFile
ChDir "C:\Users\paddy\Desktop\NEW CSV files whole CGM date ok!"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\paddy\Desktop\NEW CSV files whole CGM date ok!\" & _
"3WDL_1 (2014-08-07)10secDataTable sit.csv", _
FileFormat:=xlCSVMac, CreateBackup:=False
End Sub
To get file name, use: ActiveWorkbook.Name to get path, use: ActiveWorkbook.Path
Something like this should work as well:
ActiveWorkbook.SaveAs FileName:= ActiveWorkbook.Path & "\" & _
Replace(ActiveWorkbook.Name, "xslx", "csv"), _
FileFormat:=xlCSVMac, CreateBackup:=False
Related
I recorded a macro in excel to make some formatting changes to spreadsheets. I’m planning to run it every time I open a new export to begin working with it. I’d also like my macro to rename the file, based on the original name but adding more words.
For example:
Old file name: Sales.xls
New file name: 2019.07.10 Export Sales Backup.xlsx
The original file name will be different every time (“Sales”, “Mailing”, etc.), so I need the code to be based on the old name and not just dub everything Sales. The folder will always be the same—the file will always come from Desktop and get saved to Desktop.
I’ve read some questions about renaming but I can’t figure out how to parse the original file name to separate out the file path and only keep the bit I need. I’ve also seen questions that are about a batch rename, but I want to do this as part of an existing excel macro that I’ll already be running anyway. I’ve already read through these forums and figured out how to add the date and change the file format.
My current code says:
Sub Test()
FName = "C:\Users\Grace\Desktop\" & Format(Date, "yyyy.mm.dd") & ".xlsx"
ActiveWorkbook.SaveAs Filename:=FName, _
FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
End Sub
I don't know where to start with trying to keep the original file name. Any help would be appreciated.
This is one of my first times using VBA. I have a command button that is supposed to be saving a file as, to my sharepoint online documents page. I can use the "excel services" and save to that documents page from excel manually, but when i try the same in VBA it is saying I do not have permission. Below is the code I am using, any advice would be much appreciated!
Private Sub CommandButton1_Click()
Dim path as string
dim filename1 as string
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents"
filename1 = Range("B3").text
activeworkbook.SaveAs FileName:=path & filename1 & ".xlsx", _
FileFormat:=xlopenxmlworkbook
End Sub
If your current file is in the same folder as the destination you would like to save in, try this change for the definition of path:
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents/"
The missing final / in your code is causing the FileName argument to be invalid, because path & filename1 & ".xlsx" evaluates to
https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents[filename1].xlsx
Which means if permissions weren't restricted on the /xxxxx folder you would have written a badly named Excel workbook in that location.
ALTERNATIVE SOLUTION
Potentially another solution for your problem. Save an empty workbook in the location you wish to save your new Excel files. Open the empty Excel file, and run your macro there. Change the path line to:
path = ActiveWorkbook.Path & "\"
Try this to see if it works instead. This is how I got around Sharepoint permissions problems.
I writing a macro to save the data in Excel sheet as CSV format - VBA 2010
I just have 2 issues at the end of the macro after everything is done.
1) In the command ActiveWorkbook.SaveAs I used the option ConflictResolution:=xlLocalSessionChanges to guarantee overwriting the file if exist in the same folder with the same name, this option should overwrite the existing file in quiet mode without asking the user if he wants to overwrite the existing file or not.
It was working in the below syntax
ActiveWorkbook.SaveAs Filename:="C:\File1.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges
And if a file with the same name was exist it was overwriting
But this option is not working although it was working for example when saving the file in normal Excel format but it is not working when saving as CSV.
But it is not working in the below syntax
ActiveWorkbook.SaveAs Filename:="C:\File1.csv", FileFormat:= _
xlCSV, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges
What I mean by 'It is not working' is that it is not overwriting the existing file and still displaying the message that there is a file with the same name exists and is asking the user if he wants to overwrite it or not.
As this is a macro so I don't want any interference from the user.
2) When I use the command ActiveWorkbook.Close to close the file after converting it to CSV, I have the message 'Do you want to save the changes (Yes/No).
I also want to save the file in quiet mode without having this message.
For the first part, add Application.DisplayAlerts = False right before you do the SaveAs and add Application.DisplayAlerts = True right afterward. That suppresses the overwrite message and automatically saves over the old file.
For the item 2, Try this:
ActiveWorkbook.Close False
It will close without the question.
Jair Batista
I am trying to use the SaveAs method to save a .csv document as a .xls document. However, when I try and designate the folder, it fails.
For example, it will save to some default directory (my documents, which is not where I am ready from) here:
Sub csv()
Workbooks.Open Filename:="directory/tmp.csv"
ActiveWorkbook.SaveAs Filename:="test.xlxs", FileFormat:=51, CreateBackup:=False
End Sub
But this fails:
Sub csv()
Workbooks.Open Filename:="directory/tmp.csv"
ActiveWorkbook.SaveAs Filename:="otherdirectory/test.xlxs", FileFormat:=51, CreateBackup:=False
End Sub
Is there a different way to specify the directory?
Use FileFormat:=xlCSV
This works for me.
ActiveWorkbook.SaveAs Filename:="C:\test.csv", FileFormat:=6, CreateBackup:=False
Whenever in doubt, record a macro :)
The FileFormat Constant is xlCSV. Either you can type FileFormat:=xlCSV or you can use it's actual value which is 6
EDIT
I see that you have edited the question :) Let me go through it again :)
Is there a different way to specify the directory? Thanks,
What is the exact path that you are specifying? I used Filename:="C:\Users\Siddharth Rout\Desktop\Book1.xlsx" and it works perfectly.
You can do Siddarth suggests and specify the directory by writing out the entire path.
Alternatively if you wanted to have a user be able to change the directory the file is stored in when saving you could use:
ChDrive (filePath)
ChDir (filePath)
Where filepath would be a starting directory for use with Application.GetSaveAsFilename. The user could then type in the name of the file or change directories if need be.
My issue is this.
I read in a series of filenames using a wildcard, so that the end of the filename is unknown, and the extension is either .xls or .xlsx. So, the wildcard is something like :
beginningOfFilename_*.xls*
I then want to take each file, after I have manipulated it, and save it with the same name, but as a .csv (comma seperated value file). In vba for excel, can I just specify the format and it will take care of the extension, or do I have to somehow pull off the( unknown) extension, and append .csv
If the second case is neccessary how would you approach this problem, I don't know where to start, since part of the filename is unknown, and I am not sure how to manipulate strings in vba.
I'm a VBA beginner.
Any help will be appreciated, thanks.
The line you want is :
Mid(sFile, 1, InStrRev(sFile, ".")) & "csv"
Where sFile is the file name with any extension.
Split(sFile, ".")(0) & ".csv"
where sFile is the filename
To get a path and name of your file without extension use.
Dim StrFileName as string
StrFileName= split(ThisWorkbook.fullName,".xls")(0)
Now save your Csv using StrFileName content.
[]´s