Excel VBA: SaveCopyAs with different file extenstion - vba

I have an Excel file with .xlsb extenstion and use its macros to generate several other Excel sheets based on the contents. The macros work in a way that they change the original Excel file and then use the SaveCopyAs method to save the generated Excel sheets.
The generated Excel sheets should be saved with .xlsx extension and format.
Using the ActiveWorkbook.SaveCopyAs "C:\TEMP\XXXX.XLSX" method is not working for me because while it does change the extension it does NOT change the file format so when a user opens a generated Excel file he receives a warning message (something like "the file extension and format does not match"). The SaveCopyAs method does not have any other arguments.
How can I save the copies of my original .xlsb file with both the extension and format to be changed to .xlsx?
Note: the Workbook.SaveAs method does have a fileformat option, not sure if that helps / relevant.

Based on the hint by Zac, in my case its a better solution to copy the tab with the relevant changes into a freshly created excel file and then save it with the new filename.
ThisWorkbook.Sheets("myTab").Copy
ActiveWorkbook.SaveAs Filename:="c:\temp\xyz.xlsx", FileFormat:=51
ActiveWorkbook.Close
This is actually a lot better solution for me as the end user really need the generated tab only and not the macros or any other data in the orginal excel file.

Related

ActiveWorkbook.Save not working in Excel 2013 (prompting for overwrite)

I have problem with macro when running in Excel 2013 (macro was written in Excel 2010).
I am merging few files into one file. That file is saved with new file name (activeworkbook.saveas ...) .
After updating that final file (filtering, removing duplicates, etc.) when I use activeworkbook.save Excel 2013 is prompting for overwrite file. Even I allow overwrite, file is not saved. This problem occurred only when running under Excel 2013.
Any idea why activeworkbook.save is not working properly?
Thanks in advance
Activeworkbook is probably not pointing at the workbook you're expecting, and it's saving the wrong file. Reference the workbook directly without using Activeworkbook, perhaps using Set at the start to tidy up the code.

How to use macro enabled excel file in HP UFT?

I am using below code
import Datatable.ExportSheet path, sourcefile, destinationfile
And I am getting below error
The DataTable.ImportSheet operation failed. invalid file error
Assuming that you are getting an error message while importing a macro enabled Excel into UFT datatable
Answer is, you can't import a macro enabled Excel (xlsm) file into UFT datatable. The reason being, Datatable.ImportSheet accepts only .xls or .xlsx file extensions.
Workaround is, save the xlsm file as Excel 97-2003 Workbook (xls) format using Save As function from Excel. Or save the xlsm file as Excel Workbook (xlsx) format.

Automatically Saving Excel Workbook into another workbook

I am new to macros and I was wondering if there was a way to auto save an excel workbook into another workbook, overwriting the data. Like if the file is located in different folder. It would automatically save that workbook to a new location every time data was changed. Also, how to avoid seeing message Are you sure you want to overwrite this file?
The Workbook SaveAs command will help you a lot. Combine that with Application.DisplayAlerts = False
Make sure you put this in either a function or an Excel button.

Why does my macro crash when I save the file to a different location?

I have an Excel file in which a VBA macro loads data into a Userform.
When I copy the file to a different location the macro works fine. But if I then save the file to that location (as an Excel Macro-Enabled workbook), close it and reopen it, the macro crashes when I try to run it.
What could be causing it to crash? The macro does not make use of any external workbooks - it simply loads data from the file into the userform. I'm stumped as to what the cause is. I notice that when I save it, the file size is a bit different than the original (10 kb)
Sample code from the macro:
Public Sub LoadButton_Click()
'Policy Information
ZoneLatitudeTextBox.Text = Sheets("Saved Policy Values").Cells(2, 2)
ZoneLongitudeTextBox.Text = Sheets("Saved Policy Values").Cells(3, 2)
TownClassComboBox.Text = Sheets("Saved Policy Values").Cells(4, 2)

Save workbook along with vba code

I've created a workbook which contains some macros along with it. My task is to save it including macros so that I'll be able to run them on the saved workbook. When I try to save it in ".xlsm" format--which is a standard format to save vba code--it's getting saved however, I'm unable to open the workbook from the desired saved file. The following window is displayed
" Excel cannot open the file "file.xlsm" because the file format or extension is not valid. Verify that the file is not corrupted and that the file extension matches the format of the file"
Can someone help me with this ? I've tried using ".xls",".xltm" formats as well. But, they don't save the vba code.
Try manually changing the file extension to ".xls" and then open it. If it opens, then do a "save as" in Excel and save it as "macro-enabled" (.xlsm).