Create radio buttons in word VBA - vba

Is there a way to create a macro that, when activated, will present the user with some options, then call some other macro depends on what the user selected?
For example:
Message box: Are you a male or female?
Option 1: Male
Option 2: Female
If the user selects "Male," execute macro A. If "Female," macro B.

while in Word UI press ALT F11 to open VBA IDE
in main ribbon click Insert -> Userform
and there you have a toolbox (if not, click View->ToolBox) and a Userform canvas
from toolbox drag the option button icon and drop it into desired position of Userform. repeat it twice
select first option button in the userform, click on its caption (some "OptionButton1" is shown as default) and edit it to "Male"
do the same with second option button and edit its caption to "Female"
click twice on the first option button on the userform
it will show you
Private Sub OptionButton1_Click()
End Sub
then fill it as follows:
Private Sub OptionButton1_Click()
macroA
End Sub
click twice on the second option button on the userform
it will show you
Private Sub OptionButton2_Click()
End Sub
then fill it as follows:
Private Sub OptionButton2_Click()
macroB
End Sub
this will get you started

Related

How to close a userform when another userform or a blank space is selected in excel VBA?

Whenever people clicks on the Debit Account or Credit Account fields on the userform, another userform with a Treeview will pop-out. However, the user will always have to press cancel in order to close the userform. Is there a way for the userform to automatically close when the Voucher Entry Form userform's area is selected?
If the question is "How to unload a user-form if it is not more the active object?" then the user-form in question must be modeless. Because else it cannot be deactivated without closing it's window. And because it is not possible opening a modeless user-form from a modal userform, the main user-form also must be modeless.
Example:
Do having two user-forms:
First user-form is named "MainForm" and has one button control and the following code applied:
Private Sub CommandButton1_Click()
Load SubForm
SubForm.Show vbModeless
SubForm.Left = Me.Left + 100
SubForm.Top = Me.Top + 100
End Sub
Second user-form is named "SubForm" and can be empty but has following code applied:
Private Sub UserForm_Deactivate()
Unload Me
End Sub
Then following Sub within a default module shows the main form:
Sub test()
MainForm.Show vbModeless
End Sub
Now after MainForm is shown, SubForm can be opened by button click. And if MainForm get the active form again (gets the focus again), the SubForm will unload.

Determining when the user clicks the x - VBA

I would like to programmatically decide if the user has clicked the top right "x" button while using a form in visual basic.
I have tried:
Private Sub nameOfForm_Exit()
'code goes here
End Sub
Which has been unsuccessful. Any help would be much appreciated.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
MsgBox "Good bye"
'/ To prevent user from closing the form
'/ Set cancel to True
Cancel = True
MsgBox "You can't close me!"
End Sub
You can't invent your own terms like "Exit". You have to take them in the combo boxes above (known as Events). Since your event pertains to the Userform itself, you need to choose Userform in the left combo box. You will also see all the controls your userform has, they each have a set of events.
As has been said QueryClose will refer to the red X in the top right corner. You also have Deactivate, which will happen whenever the UserForm loses focus (if it is ModeLess, meaning that a user can click the sheet behind the userform without closing it, this will trigger Deactivate) and Terminate, which occurs after the Userform closes with QueryClose.

Invoking the Activate dialog for sheets

I want to invoke the Activate dialog for sheets in a workbook such as below. This dialog can be called manually by right clicking on the arrow button at the bottom left of Excel (2013).
I tried this:
Application.Dialogs(xlDialogActivate).Show
But instead of showing the list of sheets, it shows the list of open workbooks:
How do I call the Activate dialog for sheets?
You could create your own dialog box if you want. Create a userForm and populate it with the names of the Worksheets upon activation. You can see what the user selected through the selected function, e.g.
ListBox1.Selected(i)
You can then call a sub with the name of the Sheet and activate it, e.g.
Sub ChangeSheet(SheetName)
Worksheets(SheetName).Activate
End Sub
An alternative is to show the Sheet Command bar:
Sub ShowSheets()
Application.CommandBars("Workbook tabs").ShowPopup
End Sub
Content salvaged from comments by Scott Holtzman and Davesexcel.

Macro enabled button VBA

I'm working with VBA macros and I have a macro enabled button of which I want to disable from being clicked when the workbook is first opened. Is there anyway to do this. The macro behind it just leads to another worksheet. ![enter image description here][1]
Any help would be great
Thanks
If your button is an ActiveX Command Button, put the following code in ThisWorkbook:
Private Sub Workbook_Open()
Worksheets("Sheet1").OLEObjects("CommandButton1").Enabled = False
End Sub
Change "Sheet1" to reference the sheet that contains the button and change "CommandButton1" to reference the name of the button.

VBA Make a text box in a UserForm Active when the UserForm activates?

How can I make a text box in a UserForm activate when the UserForm activates, so that the user can start typing without having to click in the text box?
What you are looking for is the TabIndex Property.
Every object on your userform has one, it is the order to which object on the userform are selected when you press the tab button. The object with 0 TabIndex will be the active object when a form is loaded also:
So with the textbox selected go to the properties pane and look for Tabindex set this to 0 and your textbox will be selected on open.
You can also set other textboxes index to 1,2,3 and on, so that if the form is being filled out you can simply press tab to go from one text box to another.
Use .SetFocus. If your textbox's name is TextBox1, the following works:
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Let us know if this helps.