I have Check-Boxes with caption of country Names and a OK button on User-form,
User will Mark required Check-Boxes and click OK to submit the form.
Expected Result: For each checked Box there is a Macro to perform.
How do I make OK button to perform macro on Selected Countries which are Check-Marked by User?
and
Is the following code correctly handle the situation? or there is other way of doing that?
If ActiveDocument.CeemeaFinallist.EasternEurope("CheckBox1").CheckBox.Value = True Then
Application.Run MacroName:="Normal.NewMacros.CEEMEA2"
Else
End If
How do I Select all Check-Boxes at once?
Try iterating over the controls and firing the macro where the checkbox is set to True:
Private Sub CommandButton1_Click()
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
' Fire macro with ctl.Caption to identify the country
End If
End If
Next
End Sub
Related
I have a userform with many comboboxes and an "Ok" button. What I need is that when pressing that "Ok" button, VBA to check if there's no empty comboboxes. If all comboboxes have some value selected - close the userform otherwise return a message box and clicking "Ok" on that messagebox returns me to the userform with no filled values lost.
I've tried all the methods I could think of:
If PackageOuterRadius = null Then
if PackageOuterRadius is nothing Then
If PackageOuterRadius.value = 0 Then
If IsNull(PackageOuterRadius) = True Then
If IsNull(PackageOuterRadius.value) Then
What I've been trying to do is:
Private Sub Rigid_Filme_Ok_Button_Click()
If PackageOuterRadius.ListCount = 0 Then
MsgBox "Select a ""Package Outer Radius!"
End If
And absolutelly nothing actually checks if the combobox is empty and keeps returning a positive (That there's a value selected)
What could be the solution to this problem? Could someone, please, help me?
If you have few ComboBoxes only, you may insert lines underneath the OK button to check if all the ComboBoxes are filled but if you have too many ComboBoxes on UserForm, you may achieve this with the help of a Class Module and to do so, follow these steps...
Insert a Class Module and rename it to clsUserForm and the place the following code on Class Module...
Code for Class Module:
Public WithEvents mCMB As msforms.ComboBox
Public Sub CheckComboBoxes()
Dim cCtl As msforms.Control
Dim cntCBX As Long
Dim cnt As Long
For Each cCtl In frmMyUserForm.Controls
If TypeName(cCtl) = "ComboBox" Then
cntCBX = cntCBX + 1
If cCtl.Value <> "" Then
cnt = cnt + 1
End If
End If
Next cCtl
If cnt = cntCBX Then
Unload frmMyUserForm
Else
MsgBox "All the ComboBoxes are mandatory.", vbExclamation
End If
End Sub
Then place the following code on UserForm Module. The code assumes that the name of the userForm is frmMyUserForm and the name of the CommandButton is cmdOK.
Code for UserForm Module:
Dim GroupCBX() As New clsUserForm
Dim frm As New clsUserForm
Private Sub cmdOK_Click()
frm.CheckComboBoxes
End Sub
Private Sub UserForm_Initialize()
Dim i As Long
Dim ctl As Control
For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
i = i + 1
ReDim Preserve GroupCBX(1 To i)
Set GroupCBX(i).mCMB = ctl
End If
Next ctl
End Sub
And you will be good to go.
i have a word with 15+ VB forms and 20-50 CheckBoxes in each.
How do i clear (if they are cheched) all the CheckBoxes in active form w/o having to write the name of each CheckBox?
Thank you
This code will do the job. It must be placed in the UserForm's code sheet.
Private Sub ClearCheckBoxes()
Dim Ctl As MSForms.Control
For Each Ctl In Me.Controls
If TypeName(Ctl) = "CheckBox" Then Ctl.Value = False
Next Ctl
End Sub
I have a workbook with about 130 sheets in it, which I select from a combobox on the main page. I populate the combobox using rowsource, from a named range which has the sheet names in it. It works great if you select one of the sheet names from the list, and it closes nicely if you don't enter anything and still press submit. I want to cover the case where the user types in something not on the list. I want to say something to VBA like 'if sWs is not equal to something from the named range, then show a message box which says 'error'', or whatever. Here is my code below, with my notes:
'if submit button is pressed go to selected worksheet or close the userform:
Private Sub Submit_Click()
Dim sWs As String
'the string is the text from combobox, called 'Title', selected on front page:
sWs = Me.Title.Value
'if nothing is selected then close the userform:
If sWs = ("") Then
End
End If
'If an item is selected from the dropdown list then show that worksheet and close the userform:
Worksheets(sWs).Select
End
End Sub
'If exit button is pressed close the userform:
Private Sub ExitButton_Click()
End
End Sub
I think I need to be using a Boolean, so I can specify what to do when it is false, but not sure how to write it. Sorry, I'm a bit of a noob.
Any ideas?
Try checking the list index of the item selected, -1 would indicate something has been entered that is not valid
'if submit button is pressed go to selected worksheet or close the userform:
Private Sub Submit_Click()
Dim sWs As String
'the string is the text from combobox, called 'Title', selected on front page:
If Me.Title.ListIndex = - 1 Then
MsgBox "Invalid Entry"
End
End If
sWs = Me.Title.Value
'if nothing is selected then close the userform:
If sWs = ("") Then
End
End If
'If an item is selected from the dropdown list then show that worksheet and close the userform:
Worksheets(sWs).Select
End
End Sub
'If exit button is pressed close the userform:
Private Sub ExitButton_Click()
End
End Sub
I have a form that appears on a double click event of a specific cell.
The form contains a list box with a bunch of checkboxes in it and in my _Activate() sub, the checkboxes are set to true or false based on values on the active sheet.
The trouble is that when the form opens up behind the cursor, the second click of the double click that opens the form is also checking/unchecking a checkbox in the form.
I've tried sticking "DoEvents" in the activate sub before the code sets the checkbox values but it hasn't made a difference - The checkbox behind my cursor where the form opens will be checked/unchecked.
I don't expect that the code will help much but it is essentially as below:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target = Range("aParticularRangeName") Then
frmSelectStuff.Show
End If
End Sub
Public Sub UserForm_Activate()
Dim iRegions As Integer
Dim sRecheck As Variant
Dim sRecheckList() As String
sRecheckList = Split(ActiveCell.Value, "; ")
For Each sRecheck In sRecheckList
For iRegions = 0 To lbRegionsTemp.ListCount - 1
If sRecheck = lbRegionsTemp.List(iRegions) Then lbRegionsTemp.Selected(iRegions) = True
Next
Next
End Sub
What about using Cancel = True?
If Target = Range("aParticularRangeName") Then
Cancel = True
frmSelectStuff.Show
End If
I'm having issues with an onclick event for a checkbox in a vba form. Basically what I am trying to do is ammend the value of all checkboxes on specific tab to be the same value as a master checkbox. In this instance, it is the 'Use Online' captioned checkbox below (online_toggle in the code) which once clicked should toggle the other checkboxes on the tab 'on' or 'off'. I currently have the following code but it keeps producing an error at 'For Each obj In online.OLEObjects'
Private Sub online_toggle_Click()
Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(6).Controls
If TypeOf ctl Is MSForms.CheckBox Then
If ctl.GroupName = "online_variants" Then
If ctl.Name <> "online_toggle" Then
ctl.Value = online_toggle.Value
End If
End If
End If
Next ctl
End Sub
N.B. online is the name of the tab on which all of the checkboxes are situated. If it helps the checkboxes affected by the master checkbox are all grouped as online_variants
Cheers,
Jason
In a Mutipage the page numbering starts from 0 so if you are trying to refer to the Checkboxes in the Online tab (7th Tab) then use this
Dim ctl As Control
For Each ctl In Me.MultiPage1.Pages(6).Controls
If TypeOf ctl Is MSForms.CheckBox Then
'~~> Your code here
Debug.Print ctl.Name
End If
Next