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
Related
I have migrated some Excel files, which contain Macro, in SharePoint. I am trying to get the macro runs based on the files in Excel but I am getting the following error: "run-time error 76: Path not found".
When I have the files local, this code used to run perfectly:
Sub Proceso_diarioLB()
' PROCESO DIARIO MACRO
Application.Run "'LIBRO MACROS LB.xlsm'!PROFLOSTN"
Application.Run "'LIBRO MACROS LB.xlsm'!STOFCONDN"
Application.Run "'LIBRO MACROS LB.xlsm'!COPIASTOFCONDN"
End Sub
Sub PROFLOSTN()
'
' PROFLOSTN Macro
'
'
ChDir "S:\local\007repctrlbcocoa\DOWNLOADS\data"
Workbooks.OpenText Filename:="S:\local\007repctrlbcocoa\DOWNLOADS\data\PL.txt"
How can I change the path so that it will pick the file in the SharePoint list.
Any help will be much appreciated.
There are limitations to using web based excel versus desktop versions, and one of them is that macros do not work via web based excel (eg. Opening within Sharepoint).
See Microsoft support
"A workbook in this format can be opened but macros do not run in a
browser window."
I'm making an Excel workbook for both Windows & Mac. I have a button in my workbook that allows the user to PRINT worksheets of their choice, and another that allows them to make a PDF of worksheets of their choice.
Both buttons work perfectly on Windows. On Mac 2016/2019/365, the PDF button works fine. It’s the PRINT button that sometimes causes a problem. It doesn't work properly when the user has pressed the PDF button before pressing the PRINT button. In that case, the .PrintOut line of code either gives an error, or saves a PDF file instead of printing.
If I close the workbook and reopen it, it will print just fine until I create a PDF, and then the problem happens again. I feel like this must be a bug in Excel for Mac, but am not 100% sure. I can only test it on my one Mac computer, so I know it's possible it might have something to do with my computer.
Here is some simple code to test this. I appreciate anyone who would try this on their Mac and let me know the results.
Sub PrintButton()
'this should send Sheet1 to the default printer
ThisWorkbook.Sheets("Sheet1").PrintOut Preview:=False, IgnorePrintAreas:=False
End Sub
Sub PDFButton()
'this should create a PDF file of Sheet1 on the Desktop of the Mac
Dim UserName As String
Dim FileNameAndPath As String
ThisWorkbook.Sheets("Sheet1").Activate
UserName = MacScript("do shell script ""echo $USER""")
FileNameAndPath = "/Users/" & UserName & "/Desktop/TestPDF.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FileNameAndPath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False
End Sub
The way to test it is:
Run the PrintButton sub. It should send "Sheet1" to your printer.
Run the PDFButton sub. It should create a PDF of "Sheet1" on your desktop.
Run the PrintButton sub again. This is where the problem is happening for me. I am curious if it works properly for you, or if you receive an error? I received Run-time 1004 error. (I have seen in some circumstances that instead of it printing the sheet here or throwing the error, it makes a PDF when it gets to the .PrintOut line.)
Using Excel 2010 on Windows 7. I have a VBA macro that saves the first worksheet of an excel workbook (.xlsm) into a CSV file. This had mostly worked in the past. Recently, I get error messages per the picture below, which state "Run-time error '1004': Microsoft Excel cannot open or save any more documents because there is not enough available memory or disk space."
So a couple things:
A common suggested solution from my Google searching is to set the file's location as a Trusted Location. This did not work.
I have enough disk space. That can't be the issue.
I'm not sure exactly what is meant by "available memory," but if it in any way refers to the Physical Memory figure listed in Windows Task Manager, that figure is 75%. The ultimate CSV file itself tends to be about 1,500KB.
I am always able to save this worksheet as a CSV manually without encountering any error messages, but when I do it via this VBA macro, I get the error message.
Picture of error message
My excel VBA save-as-CSV macro:
Sub saveAsCSV()
Application.DisplayAlerts = False
ThisWorkbook.Sheets("Sheet1").Copy
ActiveWorkbook.SaveAs Filename:="dummyfilename.csv", FileFormat:=xlCSV, CreateBackup:=True
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
Try this, you don't have to use Copy or Activate. If your code is in the workbook that you want to save as a workbook you can use ThisWorkbook. Kudos to #Variatus for identifying need for a path.
ThisWorkbook.Sheets("Sheet1").SaveAs ThisWorkbook.Path & "/" & "dummyfilename" & ".csv", FileFormat:=6
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 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