Changes to VBA code to open a workbook - excel

Whilst I was using Excel 2011 I had a VBA program that opened another workbook. However, having moved over to Excel 2016 it no longer works, even after changing the filepath format (obtained from another Stackflow posting). The current code reads:
I am now getting the message `
Microsoft Visual Basic Compile Error: Syntax error
Where am I going wrong? I have tried it with Workbook and Workbooks on the 'Dim' line. The second VBA program at the bottom does open the file, so I know the path is correct

Use the following method instead
Set wb = GetObject("filepath\filename.xlsm")

Related

How do I open my Excel sheets without VBA error message?

Excel automatically updated last week. Every spreadsheet I open now comes up with error code Run-time error'53':
File not found:
/Library/Application/Application Support/Adobe/MACPDFM/MacPDF.framework/Versions/A/MacPDFM
Help and End buttons show. Help does not describe my problem.
Clicking End twice gets rids of the error message and the spreadsheet opens. I have no idea how to solve this. I am not a programmer and I have not changed any settings in Excel.
Apparently your Excelfile contains a macro, who tries to open the following file when you open an Excel worksheet:
/Library/Application/Application Support/Adobe/MACPDFM/MacPDF.framework/Versions/A/MacPDFM
When this file is not present, you get the mentioned error message.
Seen the structure of the directory, I believe the Excelfile only can be opened on a Mac-computer (Microsoft-computers don't have the mentioned directory structure).
Verify if you're working on a Mac-computer or a Windows-computer and in case of Mac, check the presence (and read permissions) of the mentioned file.

Using Octave to run Excel macro (from personal workbook)

I'm following the methods of this Mathworks post (but using Octave 6.2.0 instead of Matlab)
https://www.mathworks.com/matlabcentral/answers/100938-how-can-i-run-an-excel-macro-from-matlab
to open an Excel file (Office 365 version) and execute a macro. It works very well to a point (i.e. on a test Excel file containing a test macro). However, in practice I'm using the xlsopen(...) , oct2xls(...), and xlsclose(...) functions in Octave to create the Excel file I ultimately want to use, so I think I either have to...
(a) use Octave to create an Excel file containing a VBA macro, or
(b) adapt one of the options in the Mathworks post to be able to run a macro from the 'PERSONAL.xlsb' workbook instead. I've tried changing the 'Macro1' name to PERSONAL.xlsb!Macro1 (with 'Macro1' coded into a module within the personal workbook) but this does not execute the macro and gives the following error in Octave:
error: com_invoke: property/method invocation on the COM object failed with error `0x80020009' - Exception occurred.
Invoking the macro coded into the personal workbook directly from the active workbook is successful (even if it's a different workbook), I just need Octave to do this step.
Are either of these possible to do, or is there another alternative? Thanks in advance.

File corrupted after creating the second document based on template with macro

I have many Excel templates containing VBA code. Some of them create new documents based on other templates with VBA code. They all worked well until last Friday, but today I got a bad surprise: creating the first document based on a template with macro works well, but creating the second gets corrupted code.
I was able to reproduce the problem with two very small templates created from scratch:
double click on the first file to create the first document
double click on the second one to create another document
At this point you can see on the VBA editor that the VBA project for the second document is corrupted. Signs of corruption are wrong icons on the Wrokbook and Worksheet modules on the project explorer and message about catastrophic failure if you try to compile.
Using first the second template, then the first one will corrupt the second one that is opened.
Click here to get the files to reproduce the problem.
EDIT
If you don't want to download the files (I understand, thanks to the comment #Mathieu Guindon) you can create them by simply:
create a new Excel file
add a module with a simple sub that shows a message box
save it as a template with macro
repeat with a second file
Now you have the two files that you can use to reproduce the problem described above.
Microsoft has confirmed that the build 1905 is broken and corrupts the second Excel document created from a template with macros.
They sent me a (long) list of steps to uninstall Office 365, run a script to clear some cache, create a new configuration and reinstall specifying the semi-annual channel for the updates and prevent the 1905 from being installed.
Excel 2019 MSO (16.0.11629.20210) 32-bit.
=== Tested: Opening sequence and filetypes ===
• FAIL - Open an Excel template that contains VBA, then another one that contains VBA.
• OK - Open an Excel template that contains VBA, then Excel template with no code in any module.
• OK - Open an Excel template that contains VBA, then an XLSB (Excel binary file) that was created by resaving any of the previous code-containing workbooks.
=== Tested: VBA Editor's Immediate window ===
• FAIL - In VBA Editor, open a code module from the second or any subsequent workbook that has VBA in it, then run a command in the Immediate window.
• OK - In VBA Editor, open a code module from the first workbook, then run a command in the Immediate window.
=== Couldn't find any MS article about it. ===
Not listed on https://support.office.com/en-us/article/fixes-or-workarounds-for-recent-issues-in-excel-for-windows-49d932ce-0240-49cf-94df-1587d9d97093 but it's definitely an Excel issue:
Can't open more than one macro-enabled workbook and have macros actually run, unless the 2nd and all subsequent macro workbooks are .XLSB.
Even with no addins loaded.
Symptoms:
• Excel is configured to allow macros, but no macros run.
• Excel is configured to prompt for macros, but doesn't.
• Attempting to run a macro in workbooks after the initial workbook causes Catastrophic Error, Automation Error, or Compile Error.

VBA Module disappears after saving Excel workbook

I was just introduced to VBA. However, on saving my first excel file with VBA code, with .xlsm extension (I was advised to do so), and then reopening that saved file, I realized that all the VBA code had disappeared and the cells in which I had used the user-defined functions threw errors. Saving the workbook as .xlsx instead of .xlsm also didn't help. When I tried saving the VBA module, it showed that FUNCRES.XLAM is read-only and hence cannot be modified, and so try saving it at a different location. Doing so, saved an excel file which on opening showed a pop-up message that it's corrupt so can't be opened. Then I gave all permissions to FUNCRES.XLAM and it is NOT read-only. But the problem still prevailed. What should I do? Can someone please help me?
PS: It is Windows 10, Office 2016.
Your VBA code needs to be saved in a module within the same workbook in which you are working. This will probably be listed as VBAProject(Book1)
There will likely be other projects listed in the Project Explorer window of the VBA GUI, but your code will not get saved with those modules.
These are related to add-ins, and not to your workbook.

Unable to Close Excel Workbook without SaveAs Prompt

Hi there I have thorny little problem thats causing me a major headache, I have a section of code in VB.NET where I need to close the current Excel workbook, however whenever I make the call to:
'Now force a close of the active workbook without saving
_myXLApp.Workbooks(1).Close(Excel.XlSaveAction.xlDoNotSaveChanges)
The SaveAs dialog gets displayed... How do I prevent this from happening? My understanding of the code is that making the call as above should close the workbook without any SaveAs prompt. Any assistance much appreciated.
Kind Regards
paul J.
Try this instead:
_myXLApp.Workbooks(1).Close(SaveChanges:=False) 'Instead of Excel.xlSaveAction.xlDoNotSaveChanges
This should do the trick. Any time I've closed Excel workbooks in VB.Net this is the command I use. It actually mirrors the Excel VBA Format.

Resources