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.
Related
Good day,
My Google-fu has failed me as I've searched and am unable to locate the information I require.
I have a master workbook, when it is ran, it will create another workbook based on cell values and the master closes and remains unchanged, the new workbook is then used to store information and is saved to an Archive folder when the workbook is closed. This archived workbook can be opened in future to edit or resend data but if the master workbook is opened it starts all over as blank and as stated creates a new workbook again.
It has 5 modules, 14 userforms and also uses code in ThisWorkBook and some save codes on different worksheets throughout.
When I update something, like add or modify a userform, I email the change out to 13 different emails and attach the master workbook which they download and replace it and carry on. The workbook has to remain as one file and has to run without internet access as they're MS Surfaces that are used in an outside work environment and internet access is not always available.
I have found some things regarding this but they usually only export and import bas.
I would like to know if anyone knows of a way or a source to update all the modules and userforms in workbooks in Archive.
The user will download the master from the email, replace the previous and when it is ran it will pull the updated forms and code from it and export them to the older workbooks in the Archive folder so the new additions can be used in the older workbooks. I also require that the information on the worksheets in the old workbooks remains unchanged.
The master would need to update multiple workbooks at first run, then not run again until the next update when it replaced again. I can do this by using a cell value like if A1 = 1 then don't run for example.
I haven't tried anything yet as I only found the bas export, importer as stated.
I apologize for my wall of text and I hope I am clear on my scope.
Thank you for your time.
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?
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.
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.
I have a reasonably complex macro that I need to run on multiple different excel sheets, this macro is updated periodically and whenever a change is made its necessary to change it in each individual excel sheet. is there a way to get each excel document to refer to the one macro?
for example if i had a hierarchy like this:
DOCUMENTS:
-xlsheet1.xls
-xlsheet3.xls
-xlsheet2.xls
MACROS:
-macro1.bas
where there was a button in each sheet that ran macro1 when clicked.
I would recommend either moving that macro to your personal file or create an Add-In
Working with Personal File
Topic: Create and save all your macros in a single workbook
Link: https://support.microsoft.com/en-us/office/create-and-save-all-your-macros-in-a-single-workbook-66c97ab3-11c2-44db-b021-ae005a9bc790
Quote from the above link:
When you first create a macro in a workbook, it works only in that workbook. But what if you want to use the macro in other workbooks? To make your macros available every time you open Excel, you can create them in a workbook called Personal.xlsb. That’s a hidden workbook stored on your computer, which opens in the background every time you open Excel.
Creating an Add-In
Topic: Creating Excel Add-ins
Link: http://www.ozgrid.com/VBA/excel-add-in-create.htm
Quote from the above link:
I am often asked by users 'what is the best way to distribute their macros?' My answer, is without doubt via an Excel Add-in. After all, this is what Add-ins are for. For those that are not sure what an Excel add-in is, it's is nothing more than an Excel Workbook that has been saved as an Add-in, File>Save as \ Microsoft Excel Add-in (*.xla). Once saved and re-opened the Workbook will be hidden and can only be seen in the "Project Explorer" via the Visual Basic Editor. It is NOT hidden in the same way as the Personal.xls as this can be seen (and made visible) via Windows>Unhide.
The Personal file is good for having a macro across any number of workbooks on a single computer. In a networked environment with multiple users, you could simulate the Personal file by having a single workbook with your macros in it and coding all other workbooks to open and hide this workbook when they start up.