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).
Related
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.
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.
When I attempt to open a certain target .xls file using the Workbooks.Open command in an Excel VBA macro, the target Excel file opens with a "1" appended to the end of the file name. This altered filename shows up in the file window (). With each subsequent attempt (manually closing the target Excel file & re-running the script) the appended number at the end of the target filename increments by one.
The target Excel file itself does not appear to be corrupted, as I am able to open the file manually in windows explorer without any issue. Also, when I do open it this way, the filename shows up correctly (without the '1' appended to the end). Looking at the 'Type' column in the Explorer window, it shows that the target file is 'Microsoft Excel 97-2003 Worksheet', so the file extension does match with the actual file format.
Below is the code that I am using:
Public Sub Import_TD_TVs()
Dim ActiveWB As Workbook
Dim srcTDWB As Workbook
Dim wkbkStr As String
Set ActiveWB = ActiveWorkbook
wkbkStr = "X:\sites\DTS_Data_Files.xls"
Set srcTDWB = Workbooks.Open(wkbkStr, , False)
End Sub
Any assistance on this issue would be greatly appreciated!
This can happen if the target workbook was originally saved as a template (.xlt) file and then the extension manually changed (to .xls) in Windows Explorer. You can open the target workbook and do a 'save as...' to .xls file format.
I have an .xlsb file, which I'm running on a different computer, after defining the paths variable and dependent on its current location. I added the VBA Project of a different xlsm file to the .xlsb. And I'm trying to run a macro from Module 2 of the .xlsb file with either one of the following lines in the following code:
Dim path As String
path = Application.ActiveWorkbook.path
path = path & "\"
Workbooks("2cDataSet.xlsm").Activate
'MsgBox (path & "2cDataSet.xlsm!publicdatahandler")
'Application.Run (path & "2cDataSet.xlsm!publicDataHandler")
'Application.Run (path & "'2cDataSet.xlsm'!publicDataHandler")
'Application.Run (path & "2cDataSet.xlsm!Module2.publicDataHandler")
'Application.Run (path & "'2cDataSet.xlsm'!Module2.publicDataHandler")
'Application.Run ("2cDataSet.xlsm!publicDataHandler")
'Application.Run ("'2cDataSet.xlsm'!publicDataHandler")
'Application.Run ("2cDataSet.xlsm!Module2.publicDataHandler")
'Application.Run ("'2cDataSet.xlsm'!Module2.publicDataHandler")
'Application.Run "2cDataSet.xlsm!publicDataHandler"
'Application.Run "'2cDataSet.xlsm'!publicDataHandler"
'Application.Run "2cDataSet.xlsm!Module2.publicDataHandler"
'Application.Run "'2cDataSet.xlsm'!Module2.publicDataHandler"
'Application.Run path & "2cDataSet.xlsm!publicDataHandler"
'Application.Run path & "'2cDataSet.xlsm'!publicDataHandler"
'Application.Run path & "2cDataSet.xlsm!Module2.publicDataHandler"
'Application.Run path & "'2cDataSet.xlsm'!Module2.publicDataHandler"
'Run macro:="2cDataSet.xlsm!Module2.publicdatahandler"
'Run macro:="2cDataSet.xlsm!publicdatahandler"
It appears to be the same problem as here, except I expect the problem in this case to be caused by the fusion of the 2 different excel files rather than the un-enabledness of the macro's:
Cannot run the macro... the macro may not be available in this workbook
Because as it suggests I have manually enabled the macros and object security settings by:
Click the Microsoft Office Button, and then click Excel Options.
Click Trust Center.
Click Trust Center Settings.
Click Macro Settings.
Click to select the Trust access to the VBA project object model check box.
Click OK to close the Excel Options dialog box.
Close and re-open excel.
I also clicked the "enable macro buttons above that objectmodel checkbox.
I tried first manually opening the 2cDataSet.xlsm first and manually enabling the macro's when prompted, and then opening the parent .xlsb, and the other way round (where the .xlsm is automatically opend without prompting for enabling macros after enabling the macros for the .xlsb)
And I think I've tried the right syntax in any of the options tried. *By now I'm also aware the single ' is only used in case the name contains spaces.
I have not deleted the macro and recreated it.
I checked whether the "option explicit" wasn't written as "options explicit" but it wasn't written at all
The sub is public.
The office I wrote it in was 2016, the version on the other pc is 2013, but I got the same error in the 2016 office, but for some, to me unknown reason, sometimes after switching the calling of the sub syntax or re-opening the excel file, the error did not occur anymore or not consistenly.
Any suggestions on why excel gives the following error:
cannot run the macro the macro may not be available in this workbook
or all macros may be disabled
after the sub is called?
Ps. It's for a distributed automated computation, so I will not be able to edit pc (or excel) settings, and popups are a nono :)
The problem was that after merging the vba projects, excel kept the reference to the .xlsm file to the old folder even though the parent .xlsb file was copied and moved to a different path.
The solution is currently being verified as being: Moving the original file into the folder of the new parent file, and sharing that entire folder with the actual .xlsm included, to be run on a different computer
I added the VBA Project of a different xlsm file to the .xlsb.
You have to open the *.xlsm like this:
Sub openFile_RunMacro()
Workbooks.Open "File2.xlsm" 'if File2.xlsm is in the same path as File1.xslb
'otherwise » Workbooks.Open path & "File2.xlsm" «
Application.Run ("File2.xlsm!Test")
End Sub
If the message for activating macros is annoying you, try to set the used path as a trusted location.
Edit
I'm trying to run a macro from Module 2 of the .xlsb file with either one of the following lines in the following code
So you're trying to run code from the .xlsb-file? Then you've to change the filename in your code from .xlsm to .xlsb.
I have two files. In the first i wrote a macro that takes as input the second file and open it to read its datas. The problem is that every time i launch the macro the second file open and i don't want that. I think the problem is this:
path = "C:\Users\Me\Desktop\example.xls"
Set openWb = Workbooks.Open(path)
The Workbooks.Open(Path) that open the file. How can i solve the problem? I need only take the example.xls file and read its data without open that file in summary. Thanks a lot