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.
Related
My vba script in myMacro.xls Workbooks.Open Method work well as below,
Workbooks.Open Filename:="D:\ExcelMacroProj\myTest.xls", ReadOnly:=True
But when I try to change the Filename value to a new path as below, but all my practices didn't work. Show Run time error 1004.
Workbooks.Open Filename:="myTest.xls", ReadOnly:=True
or
Workbooks.Open Filename:="./myTest.xls", ReadOnly:=True
or
Workbooks.Open Filename:=".\myTest.xls", ReadOnly:=True
Actually myMacro.xls and myTest.xls was placed in the same folder. That's why I want to change to a flexible folder directory.
how could I fix this issue? Appreciated for your read and reply.
You might try using ThisWorkbook.Path to make an absolute path. It returns the folder path of the workbook running the macro (excluding the filename). Something like this should work:
Workbooks.Open Filename:=ThisWorkbook.Path & "\myTest.xls", ReadOnly:=True
Make sure to include a backslash, since the workbook path doesn't end with one.
Filename is relative to the current Excel directory (which is different from the directory in which an opened document is).
You change the current directory by using ChDir "x:\new\path".
But what you actually want to do is:
Workbooks.Open Filename:=EnsureSlash(ThisWorkbook.Path) & "myTest.xls", ReadOnly:=True
, where EnsureSlash is your custom function that appends a backslash (\) to the end of the string, if it's not already there (because ThisWorkbook.Path ends with a slash when the path is the root directory, and doesn't otherwise).
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.
**ThisWorkbook.SaveAs Filename:=Sheets.(Sheet1).Range("B25")**
Cell ("B25") is equal to W:356487\Desktop\ExcelFiles\ABCtemplate.xlsb
The Save As portion is located at the end of my macro once the code has finished running.
ThisWorkbook.SaveAs Filename:=range("b25").value, _ FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
There was some syntax issues in your line, corrected below: -
ThisWorkbook.SaveAs Filename:=Sheets("Sheet1").Range("B25")
Also ensure that the path in B25 points to an existing folder.
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
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