CommandButton Clicking vs Executing from VBA Editor, - vba

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.

Related

UserForm Option Button Controls Crash Excel After Toggling at Runtime

I'm having a problem with radio option buttons on a UserForm I have created in excel. When the UserForm is loaded it is defaulted to OptionButton1. If I toggle to OptionButton2 and then toggle back to OptionButton1 excel will be unresponsive and crash when I close the form. If I just toggle to OptionButton2 and close the form or don't toggle them at all, everything will be fine. It only happens if I toggle more than once in a given runtime. Also, the option buttons are on a frame on the userform if that makes any difference.
I have commented out ALL code that is triggered by events attached to the controls and it still happens. Also, the code works perfectly fine in the case that I only toggle the options once. It will crash simply by changing the option to 2 and back to 1 without executing any other code. I have another option button group on the form and I don't have any issues with that group. I can toggle them as many times as I want.
I'm not able to share my actual workbook and have been unsuccessful recreating the problem in an example workbook. I'm not sure what else I can provide that would be of use. If anyone has any suggestions based on my description of the problem, it would be greatly appreciated. I have run out of ideas.
Things I've tried:
Removing all code associated with the controls
Changing and removing the group name for the set of option buttons
deleting the option buttons and remaking them
changing the names of the option buttons
copying and pasting the existing working option buttons and renaming them
EDIT: I was able to save as on my original workbook and delete everything out except the working and broken option controls. Here is a link to the sample book which is a bare skeleton of my actual workbook:
Link to workbook example.
Even with everything gone, the problem still happens. If you open the userform, you can toggle between the "Option A" and "Option B" buttons, but if you try to toggle between "Option 1" and "Option 2" excel will crash when you close the form.
Your User-Form and /or controls are corrupt. In line with what Commitern suggested, instead of full export and recreation, try this:
1.Delete your faulty user form. in this case `Test`.
2.Save and close Excel.
3. Go to C:\Users\<<yourusername>>\AppData\Local\Temp\VBE
4. Here delete all the exd files.
5. Re-open workbook add new form, name it `Test` and all option buttons.
All works fine.
Remember if you try to add a new user form and rename it as the old one Test without saving the file, you will get error.
https://support.microsoft.com/en-us/kb/244238

Excel vba: Auto run the routines

I have various routines in two different modules. Some clears the report, some populates it, fill the blank spaces with zeroes. I am currently running them using F5. But I want when the user opens the sheet, he gets all populated data without having to run that particular sub routine. Is it possible to run the routines in various modules automatically when the excel is opened?
This is pretty easy. Instead of putting code in the module, you will put it in ThisWorkbook and use the Workbook_Open event. You don't necessarily have to move your code. You can just call the existing macros.

DoubleClick captured in my UserForm

As I bet I can tell the following is happening in my Excel 2013 macro:
Sub WorkSheet_BeforeDoubleClick is calling a form
Some logic populates and shows the form
The double click event is captured by the form as an undesired selection in a listbox
Is there a correct way to suppress the double click in the middle of the subroutine? I have ideas for getting around it (sleeping the thread works, re-positioning the form works, locking and unlocking the listbox based on other events works) Those are all poor workarounds and I am hoping for a built-in solution that I am just not aware of.
(And setting Cancel = True does not work because I believe setting only kicks in once the code is finished running. The double click becomes a selection in the middle of the subroutines)
If the issue is the form you are launching from the macro is catching the same double click event try the following:
1) Have your macro call a new sub routine via 'Application.OnTime()' schedule it to run just long enough in the future to pass the 'physical click', maybe 1s. Instead of directly calling Form.Show(), as you are currently doing.
2) Your new subroutine just has to do: Form.Show(), and whatever else you deem necessary.

Excel VBA display dialog locked in front of screen during macro

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?

Excel AddIn - Keeping windows form always visible while w/in Excel

First of all, thank you for your time and assistance in reviewing this!...
I'm trying to upgrade an Excel VBA workbook to a VSTO Excel Add-in in VB.NET using VS 2010. In the original (i.e.- VBA) version I have a modeless UserForm (called frmMain) that floats on top and is visible at all times while the user is still within the Excel application, but is not visible if the user moves to another window outside of Excel.
For example, within Excel the user can click on any worksheet tab, select any cell, etc. and the UserForm is still visible. This is exactly how I'd like it.
The problem is, that in the new VSTO add-in, I can not get the Windows form to mimic this same behavior.
I use frmMain.Show() to show the form as a modeless form, but the moment the user clicks an Excel worksheet (i.e.- activates a worksheet) the form becomes hidden behind the worksheet.
I can manually Alt-Tab to bring the form back into view, but I need it to always remain in view - floating on top of the Excel worksheets so long as the user hasn't left the Excel application.
I tried various things, including setting the form to TopMost, however, that causes the form to be TopMost everywhere - including outside of Excel. Worse than that, if the user does anything that would normally result in Excel's launching a dialog box (e.g.- closing an open workbook, raising the alert "Do you want to save the changes...") the alert dialog box itself is hidden and inaccessible behind the frmMain form (since the frmMain is TopMost).
How can I get my form to behave in the desired way (i.e.- the same way it did in VBA)?
Thanks!!!
Rob
You should take a look at Custom Task Panes which can be docked or floated within the Excel application. You could also look into the COM interfaces for a lower level connection (see related SO Post) - although Task Panes are really what this type of behavior was intended for.
This method might work (worked for me):
Create a worksheet_selectionChange event handler inside your form class
Put these lines in this event handler:
Dim FormHandle As IntPtr = Me.Handle
Me.Visible = False
Me.Show(NativeWindow.FromHandle(FormHandle))