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
Related
I have a VB Script or Excel Macro which I created and it works fine when I run the code from within Excel as an excel macro. But when I want to run it from Windows Explorer (i.e. save the text as a VBS script), I keep getting the Expected Statement error.
The code below essentially deletes two tabs from a template that I want to use, and then copies two tabs from another workbook to the template, and then saves that file. The code below was created using the record button in Excel. I am a total newbie to VBA, and so I do not know why the error is occurring. I do not also want to run the file from within Excel, because I want to be able to change the text what is in the macro. Thank you for your help.
Sub mapping_specs_comp_ss()
Set OrigMapTemplate = Workbooks.Open("D:\Kriss\Mapping Specs\Mapping Specs Template Version 3 - Copy Final.xlsx")
Application.DisplayAlerts = False
OrigMapTemplate.Sheets("QSTEST Mapping").Delete
OrigMapTemplate.Sheets("QSORRES Mapping").Delete
Set closedBook = Workbooks.Open("D:\Docs\Mapping Specifications\Automation\Mapping_Create - QSTEST QSORRES.xlsx")
closedBook.Sheets("QSTEST Mapping").Copy After:=OrigMapTemplate.Sheets("Visit Mapping")
closedBook.Sheets("QSORRES Mapping").Copy After:=OrigMapTemplate.Sheets("QSTEST Mapping")
closedBook.Close SaveChanges:=False
ActiveWorkbook.SaveAs Filename:= _
"D:\Docs\Mapping Specifications\Automation\Automated_Mapping_Specifications.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
This VBA macro takes a template xlsm file saved on a shared drive and automatically uploads the file saved as with tomorrow's date in the title to SharePoint. It is triggered by a specific appointment reminder out of Outlook.
Although the file is saved to the SharePoint library, it seems to be locked for any kind of editing.
Error when trying to edit the file in browser:
Can't Lock File for Editing
Close
Error
We're sorry. We couldn't lock this file for editing. Would you like to try again?
Error when trying to edit properties in browser:
The file "http://sharepoint/TESTING FILE.xlsm" is locked for exclusive use by i:0#.w|"MyuserID".
Sub savetest()
Dim Template As Workbook
Workbooks.Open ("C:\MyFilePath\Name")
Set Template = Workbooks("Name.xlsm")
Template.SaveAs FileName:= _
"http://sharepoint/MyLibrary/MyFilePath/Name" &
Format(WorksheetFunction.WorkDay(Date, 1), "MM-DD-
YYYY") & ".xlsm?web=1" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled,
CreateBackup:=False
End Sub
**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 have a excel worksheet order form that once completed, I need the end-user to click a button to save the worksheet, before then clicking an email link for sending.
I pulled a simple save script which works fine in bringing up a Save As dialog box, but I now need to add to it:
a) the name of saved file so I can save it as Trans_Order.xls
b) to save it as an .xls so the finished worksheet ideally doesn't contain the macro when sending over email to a 3rd party
Current code is:
Sub SaveAs()
Application.Dialogs(xlDialogSaveAs).Show
End Sub
Hope you can help - would be grateful for any assistance!
The xlOpenXMLWorkbook bit saves the file as a macro-free workbook. Application.DisplayAlerts = False suppresses an alert that fires saying that the Macro elements won't be saved. It will also suppress alerts about overwriting.
Application.DisplayAlerts = False
ThisWorkbook.SaveAs CreateObject("WScript.Shell").specialfolders("Desktop") _
& "\Trans_Order.xlsx", xlOpenXMLWorkbook
Application.DisplayAlerts = True
N.B. You may need to include the full path to the workbook in the double-quotes, or it will save back to the folder of your current workbook.
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.