How do I activate an unsaved workbook using VBA? - vba

The goal of my code is to run an online report, copy the data from an exported excel file and paste it back to the main file, and then manipulate the data.
The trouble I'm running into now is copying the data from the exported excel file. The online report exports the excel file as an unsaved "Book1" with one "Sheet1" with the data in it. I cannot seem to activate the exported file so I can copy the sheet.
I've tried
Workbooks("Book1").Activate
As well as using a for loop to iterate through all open workbooks (there are only 2 open workbooks, but the for loop only loops once - telling me that it isn't looping through the unsaved workbook).
Any help would be greatly appreciated. Happy to clarify any questions as well.
Thanks

Related

How to save as excel and vba project for future use?

How can I save as xlsx my project?
I try to do my project in excel with vba but I do know how to save it for future use.
If I save as excel sheet, vba will not be saved to it, I do know how to do that save.
Is there someone who has an idea to this problem?
Please anyone can help me.
xlsx only for data sheet. When you have VBA code in your excel workbook you should save as Excel Macro-Enabled workbook (*.xlsm)
Hello Schadrack Rurangwa**,
There a couple ways to do this.
Altenative One:
save the vba file to ".bas"
1. Click on menu Tools->Macro->Visual Basic Editor
2. This will open Visual Basic Editor
3. In the visual basic editor, select menu File-> Rightclick somewhere in the module and select export file. (save your module ".bas" somewhere).
Next time you start excel and want to use your VBA-Macro, just navigate to:
Tools>Macro->Visual Basic Editor> Rightlclick somewhere in the module and import the ".bas" file.*
Altenative two:
You could save the file Excelfile as "*.xlsm"
(A file with the XLSM file extension is an Excel Macro-Enabled Workbook file)
Macros can be stored in individual workbooks or available for a user in all workbooks opened if they are stored in C:\Users\nnn\AppData\Roaming\Microsoft\Excel\XLSTART\ PERSONAL.XLSB.
For more info see https://support.office.com/en-us/article/create-and-save-all-your-macros-in-a-single-workbook-66c97ab3-11c2-44db-b021-ae005a9bc790

Passing an Auto_Open macro from one workbook to another

I have to pass an Auto_Open macro from one Excel file to about one hundred other Excel files, which are generated automatically from R-script. I really don't know the VBA code.
At first I hoped that opening the file containing the macro and then opening the rest of the files will do the work, but unfortunately it didn't happen and I assume I have to pass the VBA code to all the files.
This macro does a little bit of formatting and limits the values in some cells. I found similar subject but I had problems with modifying VBA code to solve my problem. Is there any way to do simply copy that?
One solution would be to create a template containing the "Auto_Open" Macro and use this template to generate the hundreds of files with the "R-Script"

A copy of Excel Addin is created in My Documents after saving

I have an application (running in IBM Notes, using Lotusscript etc.) that create a new Excel object via OLE automation. In my app I create a new Workbook and saving it to a temporary folder (I am saving it as Excel Worksheet w/o macros -> type 52) so that the resuting file is sth. like that:
tmp_abc1234.xlsx
Then I open the file in Excel programmatically. I have an Excel Addin (filename is "Teamoffice.xlam" loaded where some macros reside I will trigger in the custom save actionbar button. In this save method I also call the save function:
Call ActiveWorkbook.Save
Call ThisWorkbook.Save
For some reason Excel creates a file named "Teamoffice.xlsm" in the "My Documents" folder. The file itself cannot be opened in Excel ("wrong file format error") - of course, it's an Addin!
This works well if I have only one Excel document open from my application. If I open a second (or more) I run into trouble: everytime I save on of those secondary opened (or newly created) documents Excel asks me if I want to overwrite that "Teamoffice.xlsm" file.
I added VBA code to ma macro that detects if this file is there and tries to delete it. With no luck of course, as the first instance of Excel is locking that file already. Overwriting the file when aswering the Excel-question dialog works though, but I don't want that question to appear.
Excel doesn't behave like that when using it stand-alone w/o OLE.
Several questions resulting from this behavior:
Why does Excel create this file? Can I stop it from doing so?
How can I delete the file even if Excel has a handle on it or
Can I tell Excel not tohave a handle on this file (if it's there) and let me kill it?
Can I suppress the question dialog and always say "yes, overwrite it"?
Office version is 2010 - and it makes me going nuts... so thanks in advance!
p.s.: I am not that VBA pro, so please be as detailed as you can effort :-)
I noticed that
ThisWorkbook.Save
was the reason. If I omit it no file is created. Strange that this saved the Addin file as xlsm file though.

Update charts in PowerPoint in VBA

I have a couple charts in a powerpoint presentation that I want to write a macro for to update them automatically. I have a database that will export data as excel spreadsheets and I was wondering if there was a way in either excel vba or powerpoint vba to have the user input the location of the new data file and have that new data copied and pasted so that the chart will update. I am new to VBA so any help would be greatly appreciated.
You can easily do this by editing link to the ppt charts to the new file.
Simply go to the
Excel Option Button>>Prepare>>Edit Links to Files and change source of the charts to the new file.
Let me know if you still want/need to use VBA for this.
Cheers

excell vba errors saving a file after importing a worksheet with a picture

I have written VBA code that copies an entire workbook (worksheets and code) into another. This allows me to create a unique workbook containing only worksheets I need for a particular project. Everything works great except when I copy a workbook which has a worksheet with a picture. It imports OK but when I try to save the receiving file I get a message saying "Errors were detected while saving 'filename'. Microsoft Excel may be able to save the file by removing or repairing some features....". If I continue it says it can not repair the file. If I delete the picture it saves just fine. Any ideas on what is causing this and how to fix it? I tried png, jpeg, and tiff file types.
For me this error happened when I pasted in a graphic straight from ms-paint. I saved that graphic instead as .jpg and inserted it using Excel->Insert->Picture.