Concatenation and looping through Checkboxes & Boolean values - excel

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!

Related

How to Hide certain userform fields during initialization?

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

Wrong data will appear when double click

Why when I double click at list box, the data will come out the previous one? And sometimes, it just comes out the random data from the list box.
FYI, it will happen on "Amount to be Collected (Emp 4)", "Acct. Mgr(Emp 5)", "Phone (Emp 6)" and "Email (Emp 7)".
Here is the image so that you can see clearer.
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
Dim methodsOfCommunication() As String
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
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

Add the values to the Option box & Check box

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

Excel - Set values of Userform Checkboxes based on cell contents

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.

Loop Through CheckBox Controls in VBA UserForm

I have a user form in Excel VBA with a check box for each month.
Selecting one or more cause the required month to be shown on the sheet, I copy-pasted the code 12 times and it works but I'm sure there is a better way doing it with a For loop.
This is a part of my code (it goes on 12 times):
If CheckBox1.Value = True Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("1").Visible = True
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("1").Visible = False
End If
If CheckBox2.Value = True Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("2").Visible = True
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems("2").Visible = False
End If
I tried writing:
for i in range 1 to 12
and then writing my code but there seem to be a problem when I put "i" instead of the numbers.
Assuming you aren't using Tristate checkboxes, then the .Value can only be True or False, so we should be able to get away with something like this:
(Assumes your code runs inside the UserForm, so that Controls is directly accessible)
Dim mthIdx as Long
Dim nm as String
Dim c As Control
With ActiveSheet.PivotTables("PivotTable1").PivotFields("month")
For mthIdx = 1 To 12
nm = "CheckBox" & mthIdx
Set c = Controls(nm)
.PivotItems(mthIdx).Visible = c.Value
Next
End With
(The With clause isn't strictly necessary, but it's usually a good idea to resolve nested COM references as infrequently as possible)
Try this ..
Dim i As Integer
Dim sN As String
Dim chx As MSForms.CheckBox
Dim obj As OLEObject
For i = 1 to 12
sN = format(i)
Set obj = OLEObjects("CheckBox" & sN)
Set chx = obj.Object
If chx.Value = True Then
ActiveSheet.PivotTables("PivotTable" & sN).PivotFields("month").PivotItems(sN).Visible = True
Else
ActiveSheet.PivotTables("PivotTable" & sN).PivotFields("month").PivotItems(sN).Visible = False
End If
Next
I've not checked the code but this should put you along thr right path if it's not spot on though...
For i = 1 to 12
If CheckBox(i).Value = True Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems(i).Visible = True
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("month").PivotItems(i).Visible = False
End If
Next i

Resources