I'm developing a userform, one section of which contains three checkboxes referring to different parts of the world. Depending on the combination these enter a text value into cell C9.
I want to have the checkboxes reflect what is in the cell already when the user goes back into the userform. I've been able to do this for every other item in the userform (option buttons, textboxes, comboboxes), but my checkboxes don't respond at all, they are simply unchecked when the userform appears, regardless of C9's value.
The following code is in the userform_intialize module. Any ideas?
If wsM.Range("C9").Value = "EU-5" Then
NABox.Value = False And EUBox.Value = True And RoWBox.Value = False
ElseIf wsM.Range("C9").Value = "EU-5 & RoW" Then
NABox.Value = False And EUBox.Value = True And RoWBox.Value = True
ElseIf Sheets("Menu").Range("C9").Value = "NA & EU-5" Then
NABox.Value = True And EUBox.Value = True And RoWBox.Value = False
ElseIf wsM.Range("C9").Value = "North America" Then
NABox.Value = True And EUBox.Value = False And RoWBox.Value = False
ElseIf wsM.Range("C9").Value = "NA & RoW" Then
NABox.Value = True And EUBox.Value = False And RoWBox.Value = True
ElseIf wsM.Range("C9").Value = "Rest of World" Then
NABox.Value = False And EUBox.Value = False And RoWBox.Value = True
Else: NABox.Value = False And EUBox.Value = False And RoWBox.Value = False
End If
Thanks for any help.
Put the Me. keyword in front of the checkbox name.
May also be better to use a SELECT CASE statement instead of ElseIf.
NABox.Value = False And EUBox.Value = True And RoWBox.Value = False needs to be three separate commands. Either on separate rows, or split with a : (both examples in the code below).
Private Sub UserForm_Initialize()
With Me
Select Case wsm.Range("C9").Value
Case "EU-5"
NABox.Value = False
EUBox.Value = True
RoWBox.Value = False
Case "EU-5 & RoW"
NABox.Value = False : EUBox.Value = True
RoWBox.Value = False
Case "NA & EU-5"
Case Else
End Select
End With
End Sub
Edit - I don't think you need to explicitly declare the False tickboxes - they're False by default when the form opens.
Related
Below code pulls the values from 10 checkboxes and applies a respective Boolean check.
I'm struggling with concatenating the checkbox and Boolean names into a While loop. Anyone assist please?
If CheckD1.Value = True Then check1 = True
If CheckD2.Value = True Then check2 = True
If CheckD3.Value = True Then check3 = True
If CheckD4.Value = True Then check4 = True
If CheckD5.Value = True Then check5 = True
If CheckD6.Value = True Then check6 = True
If CheckD7.Value = True Then check7 = True
If CheckD8.Value = True Then check8 = True
If CheckD9.Value = True Then check9 = True
If CheckD10.Value = True Then check10 = True
You need to use an array Check(1 To 10) instead of individual variables check1 … check10 and something like UserForm1.Controls to access your checkboxes by a variable name:
Dim Check(1 To 10) As Boolean
Dim i As Long
For i = 1 To 10
Check(i) = UserForm1.Controls("CheckD" & i).Value
Next i
UserForm1 is the form your checkboxes CheckD1 to CheckD10 are in.
If you used Form Controls on a worksheet then it should be
Check(i) = (ThisWorkbook.Worksheets("Sheet1").Shapes("CheckD" & i).OLEFormat.Object.Value = 1)
Worked it out myself in the end, so posting the answer for others if they need.
Declared my check Booleans as an array with:
Dim check(1 to 10) as Boolean
Then looped through the checkboxes on the UserForm with:
For i = 1 To 10
If Me.Controls("CheckD" & i).Value = True Then
check(i) = True
End If
Next I
Simple for those that know, but something new learnt for today!
So I have a userform that consist of Multipagesand one of the pages contain togglebuttons which hide and unhide fields on the Userform as well as on the excel worksheet. The picture below shows the togglebutton page.
The code for HAZOP/ SIL & LOPA is the same except which fields it hides is different. Below is the code for HAZOP togglebutton.
Private Sub togbHAZOP_Click()
If togbHAZOP = True Then
Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = False
Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = False
Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = False
Frame5.Enabled = True
Frame5.Visible = True
Frame6.Enabled = True
Frame6.Visible = True
Frame7.Enabled = True
Frame7.Visible = True
HazOp.Enabled = True
HazOp.Visible = True
Else
Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = True
Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = True
Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = True
Frame5.Enabled = False
Frame5.Visible = False
Frame6.Enabled = False
Frame6.Visible = False
Frame7.Enabled = False
Frame7.Visible = False
HazOp.Enabled = False
HazOp.Visible = False
End If
End Sub
Code for Initialization at the moment but it doesn't work, gives an error saying "Run-time error 438: Object doesn't support this property or method"
Private Sub UserForm_Initialize()
WizardProp.MultiPage1.Value = 0
Me.MultiPage1.Style = fmTabStyleNone
togbHAZOP.Frame5.Enabled = False
togbHAZOP.Frame5.Visible = False
togbHAZOP.Frame6.Enabled = False
togbHAZOP.Frame6.Visible = False
togbHAZOP.Frame7.Enabled = False
togbHAZOP.Frame7.Visible = False
togbHAZOP.HazOp.Enabled = False
togbHAZOP.HazOp.Visible = False
End Sub
The issue I am having is how do I hide certain fields within the userform at the beginning of the code and based upon the user's selection using the togglebuttonswill hide/unhide because at the moment I have to click on the togglebuttons then unclick to get it where the linked fields are hidden and doing that everytime during initialization can be a nuisance.
Note that you can reduce the code like below. Replace True with togbHAZOP.Value and False with Not togbHAZOP.Value in the If togbHAZOP = True part. So you don't need to repeat the whole code.
Private Sub togbHAZOP_Click()
Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = Not togbHAZOP.Value
Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = Not togbHAZOP.Value
Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = Not togbHAZOP.Value
Frame5.Enabled = togbHAZOP.Value
Frame5.Visible = togbHAZOP.Value
Frame6.Enabled = togbHAZOP.Value
Frame6.Visible = togbHAZOP.Value
Frame7.Enabled = togbHAZOP.Value
Frame7.Visible = togbHAZOP.Value
HazOp.Enabled = togbHAZOP.Value
HazOp.Visible = togbHAZOP.Value
End Sub
Since togbHAZOP is a toggle it does not have a frame togbHAZOP.Frame5 that's probably the issue here. It should probably be something like:
Private Sub UserForm_Initialize()
WizardProp.MultiPage1.Value = 0
Me.MultiPage1.Style = fmTabStyleNone
Me.Frame5.Enabled = False
Me.Frame5.Visible = False
Me.Frame6.Enabled = False
Me.Frame6.Visible = False
Me.Frame7.Enabled = False
Me.Frame7.Visible = False
Me.HazOp.Enabled = False
Me.HazOp.Visible = False
End Sub
I have working code to show and hide some text boxes etc, however I wanna have the text which is filled in one of those text boxes appears in a cell. Actually 2 text box values separate by a comma in one cell.
Private Sub CheckBox2_Click()
If CheckBox2 Then
ComboBox151.Visible = True
TextBox227.Visible = True
TextBox228.Visible = True
Label358.Visible = True
Label359.Visible = True
TextBox_combi.Visible = True
comments.Visible = True
Else
ComboBox151.Visible = False
TextBox227.Visible = False
TextBox228.Visible = False
Label358.Visible = False
Label359.Visible = False
TextBox_combi.Visible = False
comments.Visible = False
End If
End Sub
May I know, how to make the option box and checkbox checked as in the list box? Let say, if the data is yes so that the option box will automatically be checked and another one if the choose Whatsapp and email it will automatically be checked at the WhatsApp and email.
The column from Method is starting from column C9 and for Participation at column D9.
FYI,
Emp 2 - Yes Emp 3 - No Emp 8 - Whatsapp Emp 9 - Phone Call Emp 10 -
Facebook Emp 11 -Email Emp 12 - SMS
And here is the coding that I already try
Private Sub lstEmployee_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'dim the variables
Dim i As Integer
On Error Resume Next
'find the selected list item
i = Me.lstEmployee.ListIndex
'add the values to the text boxes
Dim methodsOfCommunication() As String
Me.Emp1.Value = Me.lstEmployee.Column(0, i)
Select Case Me.lstEmployee.Column(2, i)
Case "Yes"
Emp2.Value = True
Emp3.Value = False
Case "No"
Emp2.Value = False
Emp3.Value = True
End Select
' Reset Methods of Communication checkboxes.
Emp8.Value = False
Emp9.Value = False
Emp10.Value = False
Emp11.Value = False
Emp12.Value = False
' Set Methods of Communication checkboxes.
methodsOfCommunication = Split(Me.lstEmployee.Column(1, i), ", ")
For i = LBound(methodsOfCommunication, 1) To UBound(methodsOfCommunication, 1)
Select Case methodsOfCommunication(i)
Case "Whatsapp"
Emp8.Value = True
Case "Phone Call"
Emp9.Value = True
Case "Facebook"
Emp10.Value = True
Case "Email"
Emp11.Value = True
Case "SMS"
Emp12.Value = True
End Select
Next
Me.Emp4.Value = Me.lstEmployee.Column(3, i)
Me.Emp5.Value = Me.lstEmployee.Column(4, i)
Me.Emp6.Value = Me.lstEmployee.Column(5, i)
Me.Emp7.Value = Me.lstEmployee.Column(6, i)
Me.Emp13.Value = Me.lstEmployee.Column(7, i)
Me.Emp14.Value = Me.lstEmployee.Column(8, i)
Me.Emp15.Value = Me.lstEmployee.Column(9, i)
On Error GoTo 0
End Sub
Dim methodsOfCommunication() As String
Dim i As Integer
Me.Emp1.Value = Me.lstEmployee.Column(0, i)
Select Case Me.lstEmployee.Column(2, i)
Case "Yes"
Me.Emp2.Value = True
Me.Emp3.Value = False
Case "No"
Me.Emp2.Value = False
Me.Emp3.Value = True
End Select
' Reset Methods of Communication checkboxes.
Me.Emp8.Value = False
Me.Emp9.Value = False
Me.Emp10.Value = False
Me.Emp11.Value = False
Me.Emp12.Value = False
' Set Methods of Communication checkboxes.
methodsOfCommunication = Split(Me.lstEmployee.Column(1, i), ", ")
For i = LBound(methodsOfCommunication, 1) To UBound(methodsOfCommunication, 1)
Select Case methodsOfCommunication(i)
Case "Whatsapp"
Me.Emp8.Value = True
Case "Phone Call"
Me.Emp9.Value = True
Case "Facebook"
Me.Emp10.Value = True
Case "Email"
Me.Emp11.Value = True
Case "SMS"
Me.Emp12.Value = True
End Select
Next
Hey so I am a beginner with VBA but here is my dilemma:
Creating an excel userform with VBA -
I have a combobox with the numbers 1-8, the number of textboxes shown for the user to fill out is based off of the number selected in the combobox.
I also have a checkbox which, when "TRUE", enables the user to create a custom title for the output of the textbox.
Ex:
Number of Goals = 2
Custom Label Goals? [x] (True)
Goal 1 : $1,000,000
"New Home"
Goal 2 : $50,000
"Boat"
My issue is that I cannot get the number of custom label boxes to be the same as the number of goals selected. So when Number of goals is 2, 8 custom goal textboxes still show up if the checkbox is TRUE.
I found a way using the code below, but when I enter the second IF THEN statement, the first no longer responds to the clicking the checkbox:
Private Sub cbIMPLBL_Click()
If cboIMP.Value = "1" And cbIMPLBL.Value = "True" Then
txtIMPLBL1.Visible = True
txtIMPLBL2.Visible = False
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
Else
txtIMPLBL1.Visible = False
txtIMPLBL2.Visible = False
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
End If
If cboIMP.Value = "2" And cbIMPLBL.Value = "True" Then
txtIMPLBL1.Visible = True
txtIMPLBL2.Visible = True
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
Else
txtIMPLBL1.Visible = False
txtIMPLBL2.Visible = False
txtIMPLBL3.Visible = False
txtIMPLBL4.Visible = False
txtIMPLBL5.Visible = False
txtIMPLBL6.Visible = False
txtIMPLBL7.Visible = False
txtIMPLBL8.Visible = False
End If
End Sub
Any help here would be greatly appreciated.
As I am just beginning is there a different way I am supposed to be doing this, e.g. not writing all of this out individually, but as a control or function (not sure what the term would be) of some sort?
Thank you so much!!!!
Currently both if statements will fire. So if cboIMP.Value = "1" then it first runs through the first if statement and unhides the first text box. But it immediately runs the second if statement and because it does not = "2" it re hides the text box.
There are two choices a select case statement or else if statements.
Select Case:
If cbIMPLBL.Value = "True" Then
Select Case cboIMP.Value
Case 1
'hide/unhide for 1
Case 2
'hide/unhide for 2
Case Else
'Hide all
End Select
End If
Else if
If cbIMPLBL.Value = "True" Then
if cboIMP.Value = 1 then
'do your stuff
Else if cboIMP.Value = 2 then
'do your stuff
Else
'hide everything
end if
End If
This way once the code finds a true it ignores all others. If it does not find any true statements it runs the else.