This is my first post, so please be kind.
I have an Excel 2010 VBA macro inside File1.xlsm, which needs to open a second Excel file, File2.xlsx, wait for the second file to be manually edited by the user and closed before it continues. I have no problem opening the second Excel file, but have not been able to figure out how to get the macro to wait until the second file is closed to continue.
Having done some research on this, I tried to adapt suggestions from other similar posts that used Sleep/IsFileOpen in a loop or brought up a message box or user form to temporarily halt processing of the macro. In all of those cases, the second file was opened, but focus was always maintained on the first file that contains the macro. The end result is that I had to close the user form or message box without being able to edit the second file. In the case of the Sleep loop, the Sleep caused all of Excel to Sleep and the application went into an infinite loop checking to see if the file was still open followed by sleeping for 1 second.
Sorry in advance, but the complete listing exists in a closed space and is too complex to re-create here for posting.
I thought this would be simple to start a macro running, have it open a second Excel file, have the user edit/save/close the file and then continue the macro using the updated file. Any suggestions would be appreciated.
Related
This question already has answers here:
How can I save VBA watches manually or add them via code?
(2 answers)
Closed 9 months ago.
I've been learning all the nifty tools I have to debug in the VBA editor for excel, but I can't figure out if there's a way to have watches stay set for a project even if its closed/reopened until I remove them. Im doing a lot of stepping-in, etc. while Im learning, and while having watches really helps, its annoying that every time I close/open a file, I have to reset all the watches. This is really annoying when I have several variables I want to keep track of while debugging, say, a Workbook_Open event and I'm closing/opening a lot!
Is there a way to turn on watches for a module or project so they come back on when the project is closed and re-opened? Or do I have to set them manually every...single...time...?
I have a similar requirement.
My proposal was "print" variables in columns in new sheet [with name LOG]. I can save values / times and result and shared that with my team.
Steps:
Define breakpoints
Insert new line in LOG sheet
Save Values and date time
Compare results
Regards
I am opening an excel file in my code in 2 different places, with basically the same code. However, in one place it takes about 5 seconds and in the other 5+ MINUTES. I have tried opening in read-only mode in second place but no difference. Screen updating, calculation modes, events etc setting are all set to optimum and same for both procedures. Any ideas?
Procedure OK code:
Workbooks.Open Filename:=wFolder + "\" + lstVals(i)
Procedure Not-OK code:
Workbooks.Open Filename:=wFolder + "\" + lstVals(i)
Both procedures are also similar, taking in same input parameters, and are being invoked from the same form. Both are public Subs. Both open the file inside an if condition and a Do While Loop.
#OK basically opens this file as well as another, and adds the other file content to this file.
#Not-OK opens this file, does some editing to create a new excel which I save.
Time difference is in opening of the file, I have put in print statements before and after both invocations to narrow it down. I have also tried invoking both procedures in the same run, separately, one before/after the other, with other similar (different date and data) input files. Nothing makes any difference.
Have you tried to add a line like this:
DoEvents
before each invocation? In one case there may be some uncompleted actions that need to occur before the workbook can be opened.
Also, I'm not a big fan of Workbooks.Open, I would prefer:
Dim wb as Workbook
set wb = ThisWorkbook.Application.Workbooks.Open …
This way you are explicitly setting an object to the workbook you are opening and can then reference that object when reading/writing to the workbook. I've had troubles with other peoples' code when it was hard to decide which workbook was being referenced, which worksheet was the 'Active' sheet, etc.
The delay could be a result of the app pointing to a different active workbook or sheet in each case. By explicitly associating the open workbook with and object you eliminate the ambiguity.
I found the solution, though still cannot understand what the problem is.
High level flow of both procedure is similar (to the WB open point) - define vars, turn off updates, assign initial values, start loop, EXTRA processing for NOT-OK, open file, process...
EXTRA processing is where I get a region-to-process-for as user input. I don't need this region for the OK procedure. Also, I need to grab the region separately for each file I process in NOT-OK procedure.
If I move the region-capture dialog AFTER the file open, everything works fine. File opens quickly, then I get the region and process.
Anyone has any idea why a simple dialog box based capture should cause file open to take long? Also, I am capturing this region for file processing in other procedures as well. It doesn't seem to be causing problems anywhere else.
Thanks to everyone who helped! My apologies if I should have mentioned the dialog box at the outset. It is a simple InputBox call which I use in multiple places and I didn't think it made a difference.
I have a very hard problem with a project that took me a lot of time to create.
I am desperate at the moment.
I know I was a fool, but now it's just time to find a solution.
In the Workbook.Open() I added a line that calls a function. But unfortunately this function has an infinite loop.
Now i must find a way to open the workbook without running the macro, but then open the VBA code and modify that damn function or least comment that damn line, in order to continue my work.
Please. Help. I need you. I'll send you a reward if you can help me.
Hold Shift while opening your workbook
Another option would be copying the file to another folder, a non-trusted path so VBA won't activate by default when you open it.
I have an XLSM file which contains 2 Web Query connection. When I go to "Connections" in the "Data" tab I am presented with the two connections I have.
For each of these I can edit some properties, one which says "Update every X minutes". I've set this to 1 minute and also ticked "Activate background update".
This, however, won't work as the web query connections aren't run anyway.
Ultimately what I need is to run these connections automatically once every hour. Preferably without any user interaction and without the document being open.
Is this possible?
You can't refresh a connection without the file being open. You can run queries on opening the file or create a VBA routine that opens the file, then uses the RefreshAll and saves over the original file every hour.
You can use VBS too.
The final solution by OP:
I ended up making a little VBS script to handle the open, refresh,
close. Then a batch script wrapper to handle running the VBS script
and logging. Finally Windows Task Scheduler to run the batch script
periodically.
I'm invoking a macro within an Excel document via Powershell.
To invoke the macro, I have to run a named macro and call it in run. However, when the macro is invoked and completes successfully, a MessageBox will appear. As far as I know, this messagebox is the only way to find out if the process has completed successfully.
I have no control over the ability to remove the message box. The powershell script must wait for the macro to finish.
From the document: The documentation for this does not give an option for this situation, or so it appears.
Alternative Options I can work with: (But I'm not sure how to get to the point where the messagebox would be dismissed)
The Excel document can have code inserted within it via Powershell
Options can be changed within the document memory space
Is it possible to run a macro asynchronously and to check back on the execution of the macro?
There are a few convoluted ways to go about doing this.
1) Invoke another instance of the Excel application and run the Macro using that. Then how would you know whether it's done? You pass a global variable by reference to it. And use the OnTime functionality to keep checking every few seconds if its done or not.
An example for calling another excel instance is given here: Stop VBA-Script from "freezing" while sending MDX-Query
2) You can store your script as a .VBS file. Then you call the shell to run the VB script and again check some passed-by-reference variable.
3) Use a hidden worksheet as a buffer, which gets written upon in a particular location once the asynchronous code finishes running. Again, you need some clever OnTime programming to automatically run a polling service... and more importantly, to stop running it!