Assigning a variable to a spreadsheet open by a third party software - excel

I am running into issue pulling data from a workbook that is open via SAP. The macro runs and executes multiple things in SAP and then opens the required data. The data is opened by opening an Excel window. What I am currently doing, is I assign a variable for that workbook. And use that reference throughout the entire macro.
Dim SAP_WB as workbook
'macro works within SAP and results in a workbook that pops up on the screen
Set SAP_WB=ActiveWorkbook
'macro manipulates the opened workbook and gets specific data.
That workbook is assigned to the first excel instance. The macro works great if the user has the excel tool with macros opened on the first instance as well. But if the user has multiple instances open, that is when the ActiveWorkbook does not apply. The workbook that is open from SAP has a dynamic name that I have no control over. Is there anyway to assign a variable to that workbook even though it is on another instance?

Related

With two open (identical, but differently named) Workbooks I want to ensure that the vba-code executed is from the active Workbook

I've got a problem with the execution of VBA-Code.
We are using a Template-Model to create offers for our service. Once I have opened two Workbooks, e.g. to take or compare data from that other book or to work on more than one offer, and after processing my data I want to save the Workbook I have worked on with F12 (save as).
Now it happens (by accident) that the wrong book is saved as and the VBA-Code is executed in the wrong Workbook.
How can I make sure, that the correct Workbook is saved and the process After_Save is executed in the right workbook.
Thanks for support, George
ThisWorkbook is the workbook the code is written in, while ActiveWorkbook is the workbook that has focus (is selected / is on top). Make sure you use ThisWorkbook to run the code in the workbook the code is written in.

Copy macro from personal workbook to other workbook

I need to copy module1 from my personal workbook to another workbook programmatically. I get a report generated by our ERP system that is just a bunch of data in an excel spreadsheet and the macro in my personal.xlsb formats, sorts, and filters the data to make it usable. It's a lot of steps, so I just wrote a macro to do the whole shebang, and I saved that macro in my personal because this report is generated whenever we run it in the ERP system, i.e. it has no home and it's a unique file every time we get it.
I need the person I'm making this for to be able to run a macro and have the macro:
copy module1 from personal.xlsb and put it in the report
save the report as the day of the week we ran the macro on it.
I can't run this code without getting an error. The most common one I see is that it can't access TRANSCT.xls
I have changed the trust center settings to trust access to the VBA project object model. I ran this code once on my computer and it ran fine. Every time after that on my computer and other computers it does not work.
Workbooks("PERSONAL.XLSB").Activate
Dim wbkSource As Workbook, wbkTarget As Workbook
Set wbkSource = Workbooks("PERSONAL.XLSB")
Set wbkTarget = Workbooks("TRANSCT.XLS")
wbkSource.VBProject.VBComponents("module1").Export ("c:\tmp\\MrXL1.bas")
wbkTarget.VBProject.VBComponents.Import ("c:\tmp\\MrXL1.bas")
The name of the report is always TRANSCT.xls
It does not open as read-only
The module in personal.xlsb is in fact named Module1
I'm losing my mind here guys.

Opening Excel workbook containing macros (.xlsm) using ADO and getting macros to run

I've been using ADO to process many Excel .xls and .xlsx workbooks without any problems. Recently I tried to process a .xlsm workbook to load to an Access database. The workbook contains several macros that are run every time a user opens a desktop version of Excel. The macros update "yesterdays" data with "todays" data. Unfortunately when I read the workbook programmatically ADO is returning "yesterdays" data. This means to me that the macros are not running when I open the workbook with ADO. Any suggestions. Hope I don't need to rewrite the code to open an instance of Excel to handle the workbook.
ADO accesses only the data saved in the workbook. Nothing vba (or any other) code does dynamically when the workbook is opened in Excel is available. Excel macros can only run when the workbook is open in the application interface.
Either your process needs to duplicate what the macro(s) are doing in order to update the sheet data or, yes, indeed, you need to first open the workbook in the Excel environment and execute the macros, then SAVE the changes to the workbook.

Excel Open 2 Workbooks at the same time

I have a workbook that I open with vba coding. There are several different userforms to input data and get reports out of this workbook.
It seems that while this workbook is open, and active, I cannot open, to look at and/or edit, an unrelated excel spreadsheet through Windows Explorer. One of my users asked me if while this program is running if she could look at (without closing the active workbook) a different spreadsheet. It has nothing to do with coding such a request into the active workbook, its more for convenience i.e. not having to close this one and open that one.
Is there anything I can do to facilitate this request? Is there some vba code I can use in my active workbook that allows or gives Excel permission to open more than one workbook?
I think you'll find it is when a Userform or any other Excel dialog-based object is open, then you cannot simply "double-click" on an another workbook to open, as Excel is busy and focus on the open dialog screen.
However, if you start another instance of Excel (Start -> All programs -> Microsoft Excel) and then open the workbook from within the new instance, then you work with additional workbooks.

Adding Module through code

I have a master workbook containing a macro which opens another workbook containing a demand forecast. The workbook opened through the macro is downloaded from a customer portal and is all new every day without possibility of editing it beforehand.
The macro then loops through the information and creates new readable and more intuitive worksheets. However, on a few of these worksheets I would like to add some event-driven code to give tooltips when mousing over or selecting cells.
Is there any possibility (without installing add-ons from vanilla Excel 2010) to add code to worksheet objects created during a macro?
The layout of the processed workbook is more or less static, so I was wondering if I should create a template file and then copy the input into it. That would allow me to have coded the events before data is added. Is this the best possibility?
As Dan pointed out, you could use Application.VBE.ActiveVBProject to programmatically add code modules to a workbook. But doing this requires more lenient Macro security settings (which are set to untrusted, by default) and this is not recommended.
When I have to do something similar, I use three workbooks:
The data containing workbook
This book has no macro functionality
A template workbook containing the necessary macros
A macro enabled workbook to facilitate the transition.
Use workbook #3 to open workbook #1 and copy its data into workbook #2. Save a copy of workbook #2 and close it. Repeat this process as necessary.
It isn't the prettiest solution, but it keeps your code modular.

Resources