Excel VBA display dialog locked in front of screen during macro - vba

I've looked for this everywhere in vain.
I have a macro in one Excel file (SplitTickets.xlsm) that loops through 3 separate Excel files.
When SplitTickets is opened a dialog appears with a button to launch the macro.
Since I don't want users to mess with the excel with accidental keys or clicks while the macro is running, I would like to keep the dialog on top of all the files being opened, modified and closed, and update the label displayed when the process is over.
I tried setting it up as modal but since the macro needs to select sheets, cells and ranges, the dialog being modal blocks the rest of the macro being executed.
And turning modal off makes it disappear beneath the flurry of the macro's manipulations.
How can I keep this dialog on top without turning it modal?

You can use the below code instead. I use it on regular basis when I don't want the user to interfere with the code run.
Sub Testing()
Application.Screenupdating = False
Your Code
'This is very important, or it will remain disabled.
Application.Screenupdating = true
End sub
This way the user is not aware of what is happening in the background.
Sandesh :)

Add the following line to the start of your macro:
Application.ScreenUpdating = False
This will turn off the "flurry of macro" activity you can see, so the users aren't tempted to interfere. It will also make your macro run faster, because it no longer has to redraw the screen between each step. Be sure to set it back to True at the end of the macro.
You may also want to use this option as well:
Application.Cursor = xlWait
This will turn the mouse cursor to an hourglass (or whatever "wait" cursor is set in their system) so the user knows the macro is running. To disable the hourglass, use this:
Application.Cursor = xlDefault
Beyond this, the macro should automatically prevent the user from interfering with the sheets while it is running. However, it will keep a queue of the keystrokes made while the macro is running and enter them after the macro is done.

Could you add a Formname.Show at the end of you macro to make it reappear?

Related

Is there a way to put a breakpoint when my active window changes?

Is there a way to put a breakpoint on ActiveWindow.change ? I have a macro that I run in a new, unsaved Excel file, that is supposed to open CSV file, save it as, process it, and keep it in focus. Instead of that, when the macro is done it selects the empty new file that was unsaved, and puts the CSV in the background. This happens even though I use the code below, just before End Sub:
WKB1.Activate
WKB1.Sheets(1).Activate
WKB1.Sheets(1).Range("A1").Select
The thing is the three rows of code above do set the focus correctly, but at end sub it switches back to the unsaved file.
I was thinking that if I can set a breakpoint whenever the active window name changes, I can catch when that happens, because the macro I'm working with is huge, and I can't find the bug manually.
Kind regards,
Daniel
The fix was quite stupid, but I fixed the issue by creating a new ribbon tab and run the macro from the ribbon instead of running it from VBA. Also i removed and Activate o Select commands. The macro was behaving the same, even after i removed the Activate and Select commands, the thing that fixed it was running it from a new menu.

Autoshapes and charts flick like they're having a seizure when VBA code is run on Excel Dashboard

I have built a dashboard in excel for my work over the last 4 months. The workbook is well organized, by my estimation, with database sheets, "under the hood" calculation sheets and the top, user visible data exhibition sheets. Also, I have 5 VBA modules that perform several different tasks, mostly relating to control of what the users see on screen. In some cases, there are complex chains of macros that put the right numbers, autoforms and charts in the right places.
The thing is: whenever one of these chains goes off, my autoforms and charts quickly flick in and out of screen, even though i have the screen updating off when executing them. It is a very quick flicking that happens, but it happens all over my workbook, with all of my content. It's ok with the changing content, but it happens over some lined forms that I use accross all of my sheets, with hyperlinks for navigation.
Example:
Note that this happens in very quick succession, sometimes repeating several times before the code is finished running, kind of like a seizure. The problem does not affect the functionality, but is visually disturbing, and my boss would rather not having it. I have searched the internets and have not found anything useful, so any help is appreciated.
I'm using Office 2010 on windows XP.
I am now using an adaptive code snippet to toggle or untoggle ScreenUpdating and Calculation, it goes like this:
Public Sub LudicrousMode(ByVal Toggle As Boolean)
Dim LockStatus As Boolean
LockStatus = Sheets("Aux").Range("B1").Value
If Toggle = LockStatus Then GoTo SKIP
Application.ScreenUpdating = Not Toggle
Application.EnableEvents = Not Toggle
Application.DisplayAlerts = Not Toggle
Application.Calculation = IIf(Toggle, xlCalculationManual, xlCalculationAutomatic)
Sheets("Aux").Range("B1").Value = Toggle`
SKIP:
End Sub
It was able to stop the "seizures" for most of the subs, except for those with the hiding and unhiding of rows and columns. Any idea with that?

Using PowerPoint VBA to change from a Kiosk presentation type to a Speaker presentation type

I have a PowerPoint loaded with VBA and quite a few slides. I would like to be able to manually advance the slides by pressing the {Page Down} key when I am the user running the presentation, but, I want the {Page Down} key not to work when any other user is running the presentation.
I know the following:
I have a function that easily will return the name of the user, so, I can easily compare the current user to myself and execute code based on the match/lack of match. No problem here.
I know that I can set the presentation type by using: ActivePresentation.SlideShowSettings.ShowType = ppShowTypeSpeaker.
I know that I can set the way slides are advanced by using: ActivePresentation.SlideShowSettings.AdvanceMode = ppSlideShowManualAdvance
The problem and things I've tried:
Once the PowerPoint has started, using VBA code to change the value of ShowType from the default setting of ppShowTypeKiosk to ppShowTypeSpeaker does not seem to change the way the rest of the show functions.
Once the PowerPoint has started, using VBA code to change the value of AdvanceMode to ppSlideShowManualAdvance does not actually enable me to manually advance slides (I am still only able to advance slides using the ActiveX buttons on the slide).
I have thought about using event handling to capture the SlideShowBegin event, but, apparently events can only be trapped once code has established the link to the Application object, and, making that link has to happen in code that runs by clicking an ActiveX control (or similar user action) in an already-running slideshow so I am baffled how the SlideShowBegin event would ever be trapped without an AddIn with an Auto_Open macro.
Bottom line: Is there a way for PowerPoint VBA code to switch a slideshow from kiosk to speaker mode so that slides can be manually advanced? (Or, some other way to enable me to manually advance slides while preventing all other users from manually advancing slides?)
Thanks to #SteveRindsberg, the fix is to (a) determine whether or not to switch presentation modes, then, (b) if a switch is needed, run the code to change the settings, then, (c) programmatically end the slideshow, then, (d) programmatically run the code to re-start the slideshow.
It is assumed that the PowerPoint has been saved to run in Kiosk mode by default so that the standard user cannot use PageDown or other navigation techniques to move through slides; that is, slides can only be navigated by the programmer's ActiveX controls that use VBA code to move through the slideshow. (This is handy for quiz-type slideshows, etc.)
Specifically, I have Slide 1 which includes an ActiveX [OK] button on it which the user is directed to click in order to continue; basically, Slide 1 is a title slide that gives the name of the PowerPoint. When the OK button is clicked, the code behind the [OK] button checks to see whether the user should be allowed to change the presentation mode from the default Kiosk mode to Speaker mode. Here's the code behind the [OK] button in Slide 1:
Private Sub cmdFeedbackOK_Click()
'handle the click of the OK button on the feedback which will move to the next slide or exit if the wrong OS or Office Version (based on text associated with the [OK] button
'check for superuser
If LCase(Environ("UserName")) = "{Windows username who is a superuser}" And ActivePresentation.SlideShowSettings.ShowType <> ppShowTypeSpeaker Then 'this will check to be sure that we have not already changed the ShowType (which will have changed if the user opts to switch modes and the PPTX has re-started)
'superuser, so, change to Speaker mode
If MsgBox("Do you want to switch to Speaker view instead of Kiosk view so you can use PageDown?", vbYesNo + vbDefaultButton1, "Use PageDown?") = vbYes Then
ActivePresentation.SlideShowSettings.ShowType = ppShowTypeSpeaker 'switch from Kiosk to Speaker mode so that PageDown key will work
ActivePresentation.SlideShowSettings.AdvanceMode = ppSlideShowManualAdvance 'switch to allow PageDown to manually advance the slides
ActivePresentation.SlideShowWindow.View.Exit 'exit the show because the change in play mode and advance mode will not take effect until the show is started again
ActivePresentation.SlideShowSettings.Run 'restart the show; code in the OnSlideShowPageChange will get triggered to skip this first slide if the user has restarted the show
Exit Sub
End If
End If
ActivePresentation.SlideShowWindow.View.Next 'move to next slide
End Sub
Then, to prevent the superuser from having to view the first slide twice when the slideshow re-starts, I have added the following code to the OnSlideShowPageChange event:
SlideName = ActivePresentation.SlideShowWindow.View.Slide.Name
If SlideName = "sldTitle" Then 'we're on the first slide
If ActivePresentation.SlideShowSettings.ShowType = ppShowTypeSpeaker Then 'this will be true if the Windows user is me (see code in Slide1), and, since I've already seen that slide, just go to the next slide
ActivePresentation.SlideShowWindow.View.Next 'skip the first slide for superuser
'execute other code as desired
'use Exit Sub here if the code below does not need to run for the superuser
End If
End If
'execute other code as desired here, e.g., code for standard users
For me, Slide 1 gives a lengthy delay (maybe 10 seconds) after clicking the [OK] button before the slideshow re-starts, but, the standard user does not experience the delay so I don't mind the wait--it probably has something to do with the VBA code and large number of slides in the presentation that have numerous ActiveX controls--at least, that's my guess.
Hope this helps someone out!

CommandButton Clicking vs Executing from VBA Editor,

If you have look at these two captured images...
Basically, stepping into the next line should not change the selected tab as shown here. This is what happens if I click F5 from the VBA Editor to execute the sub
But when I actually run it using a linked command button which suppose to be doing the exactly same thing (photo shown down the thread). but what happens is this,
Stepping into the next line actually somehow changes the tab!
There is noting in the first break point line that tells excel to select another tab... why is this doing this.
if I run it using F8, actually this problems goes away.
UPDATED=============================================================
I come across some weird behaviour in the Excel VBA and can't explain why its doing it.
Sub run_simulation()
ActiveWorkbook.Worksheets("Run Simulation").Select
ActiveSheet.CommandButton1.value = True
End Sub
This subroutine selects a sheets then simulates the clicking of a button 'CommandButton1'.
When I execute this from the play icon within the VBA Editor it runs fine but when I run this 'run_simulation' subroutine from a linked button on another sheet... like this
an error returns saying that the selected sheet "run_simulation" is protected and needs to be unprotected before proceeding... the thing is it unprotects the sheets automatically when the commandButton1 is clicked.
The Worksheet.Protect method has a little-used UserInterfaceOnly parameter.
From MSDN:
UserInterfaceOnly (optional) - True to protect the user interface, but not macros. If this argument is omitted, protection applies both to macros and to the user interface.
Set this to True when setting the worksheet protection and you will be able to manipulate the worksheet objects programmatically.

Microsoft Excel 2013 ScreenUpdating fails

After running a macro for a prolonged period of time the ScreenUpdating in Excel totally stops working even when the macro finishes. I never set it to false but it behaves as if it is set to false and the only way to make Excel update the screen again is to close it out and reopen it. The VBA screen works however. I have tried to run DoEvents and ScreenUpdating=True but the Excel window does not update visually (it is not frozen). Is there anyway of clearing the ScreenUpdating buffer? I have tried manually setting it to false and true to no avail.
If you want to duplicate this error use this code
Application.OnTime (Now + TimeValue("0:00:05")), "macroName", (Now + TimeValue("0:00:10")), True
then I have the code copy and paste some values and I control when the loop stops with a countdown value in cell A1 for example.
I got this issue in my Excel 2013. For me, it was caused when I loaded a userform. It was very, very strange, however. After a lot of trial and error I found that what caused it was using a Sheets("MySheet").Activate command. I moved this command from running after I had shown and closed the form, to immeiately before I show the form (i.e., immediately when user clicks the 'show form' button.) This fixed it. Try fiddling with your sheets.activate commands.