Excel discard user event? - vba

I have an Excel sheet that updates every 1 Second (via Labview using ActiveX)and has to be visible to the user.
And I cannot set "Application.UserInteractive" to "FALSE" throughout the application as the User could use the system for other excel operations.
And I have the same problem with "WorkSheet.UserInterfaceOnly"
The problem that occurs is:
If the user has selected a range of data ("mousedown" and has not released it) and at the same time the software tries to write the data .... boop it doesn't work...
I can set it to wait until user release the mouse click.... But is there a way to prevent this problem by not allowing to detect mouseclick events or block it some other way

Well Anywho...
I found a simple solution....
Protect the worksheet and set EnableSelection to noSelection

Related

Pause VBA Macro to allow manual input of data

I have an Excel workbook macro that opens another workbook and starts copying data into it and formatting it. At one point in the process, I want the macro to pause and let the user manually enter data into the target workbook and then resume processing.
MsgBox, Application.Wait(), and Sleep are all application modal and will not let the user update anything in the other workbook while they are executing.
I found this while searching for a solution. It gets me halfway there in that I can manipulate the other sheet but only with my mouse. No keyboard presses get sent to the workbook.
Any ideas on getting all the way there?
I was thinking that I could just have two macros. The user would run one, then perform his manual tasks, then run the other. This appears to work but I would have to convert everything to globals so hopefully, someone has a better idea.
Thanks!
Depending on the macro being run to copy and paste, is the main concern with user intervention during execution of the macro getting the active cell/sheet (if being used) back to being active after the user manipulates something.
I'd recommend storing the active cell/sheet address in a variable prior to the Application.Wait() and then setting the active cell to that stored value on resume.
Without a posting of what your macro is doing though, it is hard to know if this suggestion helps your current situation.

Using Excel while macros are running

There are a half-dozen answers to this. "Open a second instance" "Have a pause" Etc. I'm not looking for that.
I'm looking for the user of the workbook to be able to manipulate the workbook while the macro is running. I've seen this working before, where the user could scroll around, change tabs, even add and remove data, all while the macro was running. Unfortunately, I couldn't get permission to look at the code (And committing CFAA violations ins't my cup of tea), so I have no idea how they did it.
How can you enable a user to edit the workbook as macros are running? For a specific example, I have Conway's Game of Life running. Users select cells to flip live/dead, then can run a macro to run the entire thing. I think it'd be nice for users to be able to change cells as the macro is running. (which is a second on select macro)
Thank you
Sorry just reread the question. I wouldn't expect the permutation to run for very long - not long enough to interrupt really.
But if it does, then the advice about using lots of DoEvents stands.
The other option is that you can use the OnTime event to have a "heartbeat"
VBA Macro On Timer style to run code every set number of seconds, i.e. 120 seconds
You can set the timer to say 3 seconds. Every time the OnTime event occurs you do one step of your permutation. In the three seconds in between they can edit.
Refactor your macro to use Events. In which case, you would have a series of event handlers (instead of one monolithic macro) to respond to various triggers. This is assuming that the macro is influenced by what the user is doing in the worksheet.
One way of (sort of) doing this is to use a Modeless Userform (UserForm.Show vbModeless)
The user form stays visible but the VBA stops running when the form is shown and the user can then interact with Excel. Then when the user clicks a button on the form the code behind the button starts running again.
So in reality the user is either interacting with Excel or interacting with the form ...

Refresh UserForm without refreshing worksheet

While working with Excel, I have many times faced the problem with Screen Refreshing.
Most of the time, in order to speed up VBA, programmers will use Application.ScreenUpdating = False. This will temporarily (until cancelled) stop screen refreshing. Unfortunately, there are cases, such as implementing progress bar, when you actually want to show some progress to the user. The problem is that the already mentioned command has stopped all screen refreshing.
This means, that even if on your UserForm you call a command to change a label (e.g. which would state Processing transaction 15 of 250) but you simply cannot get it to display.
What some people do in this case, is to temporarily enable ScreenUpdating, and right away disable it. This procedure is unfortunately unreliable, as if you have some difficult procedure (inserting complex formulas into cells) you are not guaranteed all the time that it will also refresh the label itself. Also the problem with this approach is, that you repaint not only your desired UserForm, but also Worksheet as well, and therefore this 'refresh' slows down your code quite a bit.
How can you refresh UserForm without refreshing the whole Excel?
The answer is simple.
Let's say that your userform is named frm_Main, so then in your VBA code you can simply call frm_Main.Repaint.
Simple as that!

Event when VBA code module is reset?

I am developing an excel sheet which has lots of ActiveX elements in it.
I am not using UserForm and all the data is loaded when an activeX button is pressed.
At this point a hidden excel app is created where various actions are performed.
This new app is then combined with other data etc etc.. based on what the user is doing on the front-end with the provided ActiveX controls.
My problem comes when the "project is reset", where all the objects are released from memory and everything.
If this was a user form I would have implemented "on terminate" event where I would close hidden all workbooks that I have used, basically clean after myself.
However this is not a userform and there is no way to detect when the "project is reset"(as far as I know) so the hidden excel app that I've created remain open but nothing points to it.
If I go to the task manager i see various "EXCEL.EXE" and the number of those is basically the number of times I have run the tool.
So, is there any way for me to detect when VBA is about to "stop the project" in the same was as there is "terminate" for user forms so I can delete unsused stuff and reset elements in the spreadsheet?
Thank you!
I have found that there is no corresponding event to catch this.
Best way to handle this is to create a hidden userform object which will "hold" all global variables and objects that I define. I can then implement a UserForm_Deactivate event to catch when the form is exiting and clean up after myself.

How to detect user inactivity in an Excel workbook

I want to take an action in an Excel workbook macro after a period of inactivity (hide/protect some worksheets). What is the best/simplest way to achieve this?
Í'm assuming I'll use Application.OnTime to periodically check if the user has been active. But what events should I handle to see if the user was "active" (i.e. has does something - anything - with the workbook)?
Clarification: I want to detect all activity, not just changes. I.e. including mouse clicks, selecting, copying, navigating with the keyboard, changing worksheets, ...
I'm assuming that when a UI event happens that represents user activity, I will set a variable thus:
LastActivityTime = Now
and the macro run by Application.OnTime will check this variable to see if the user has been active recently. Which events (other than SheetChange) would I need to handle to set this variable? I had kind of hoped there would be KeyUp and MouseUp events, these two would probably have been enough.
Update: I have implemented this by handling Workbook_SheetActivate, Workbook_SheetSelectionChange and Workbook_WindowActivate. Realistically this is probably enough.
I have implemented this by handling Workbook_SheetActivate, Workbook_SheetSelectionChange and Workbook_WindowActivate. Realistically this is probably enough.
I can only see two solutions -- either handle evary single event the Application object has or use GetLastInputInfo function.
One simple way is to compare the content of the workbook with that of the last time you check. I believe combining this with Application.OnTime will solve your concern.