Microsoft Visual Basic (Checkbox relationships) - vba

I have a problem :)
I am working in Microsoft Word 2013 right now. I made four checkboxes: "A", "B"
, "C" and "D". My wish is that there is a relationship between the checkboxes. So if I check "A" then I want that "B" and "C" are also automatically checked. And if I check "B" I want that "C" is also automatically checked. When I check "C" then I want that just "C" is checked. And if I check "D" then only "D" needs to be checked.
I already found the next code:
Sub SelectAll_Click()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
CB.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
End If
Next CB
End Sub
Sub Mixed_State()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name And CB.Value <> ActiveSheet.CheckBoxes("Check Box 1").Value And ActiveSheet.CheckBoxes("Check Box 1").Value <> 2 Then
ActiveSheet.CheckBoxes("Check Box 1").Value = 2
Exit For
Else
ActiveSheet.CheckBoxes("Check Box 1").Value = CB.Value
End If
Next CB
End Sub
This works for me in Excel, but not in Word (I get an error). But the main issue is that by this code all of the checkboxes are selected. That's not exactly what
I want.
I hope it is clear what I'm asking for and I hope you can help me out.
Thanks!
Kind regards.

Here is the answer (code), for anyone else in the same situation. The checkboxes have a lot of relationships with each other. I have 13 checkboxes.
Private Sub Q1A_Click()
If Q1A.Value = True Then
Q1B.Value = True
Q1C.Value = True
End If
End Sub
Private Sub Q1B_Click()
If Q1B.Value = True Then
Q1C.Value = True
ElseIf Q1B.Value = False Then
Q1A.Value = False
End If
End Sub
Private Sub Q1C_Click()
If Q1C.Value = False Then
Q1B.Value = False
Q1A.Value = False
Q1D.Value = False
End If
End Sub
Private Sub Q1D_Click()
If Q1D.Value = True Then
Q1C.Value = True
End If
End Sub
Private Sub Q1E_Click()
If Q1E.Value = True Then
Q1F.Value = True
Q1G.Value = True
Q1H.Value = True
Q1I.Value = True
Q1J.Value = True
End If
End Sub
Private Sub Q1F_Click()
If Q1F.Value = True Then
Q1G.Value = True
ElseIf Q1F.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1G_Click()
If Q1G.Value = False Then
Q1F.Value = False
ElseIf Q1G.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1H_Click()
If Q1H.Value = True Then
Q1I.Value = True
Q1J.Value = True
ElseIf Q1H.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1I_Click()
If Q1I.Value = True Then
Q1J.Value = True
ElseIf Q1I.Value = False Then
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q1J_Click()
If Q1J.Value = False Then
Q1I.Value = False
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q2A_Click()
If Q2A.Value = True Then
Q2B.Value = True
Q2C.Value = True
End If
End Sub
Private Sub Q2B_Click()
If Q2B.Value = True Then
Q2C.Value = True
ElseIf Q2B.Value = False Then
Q2A.Value = False
End If
End Sub
Private Sub Q2C_Click()
If Q2C.Value = False Then
Q2B.Value = False
Q2A.Value = False
End If
End Sub
Private Sub Q1A_Click()
If Q1A.Value = True Then
Q1B.Value = True
Q1C.Value = True
End If
End Sub
Private Sub Q1B_Click()
If Q1B.Value = True Then
Q1C.Value = True
ElseIf Q1B.Value = False Then
Q1A.Value = False
End If
End Sub
Private Sub Q1C_Click()
If Q1C.Value = False Then
Q1B.Value = False
Q1A.Value = False
Q1D.Value = False
End If
End Sub
Private Sub Q1D_Click()
If Q1D.Value = True Then
Q1C.Value = True
End If
End Sub
Private Sub Q1E_Click()
If Q1E.Value = True Then
Q1F.Value = True
Q1G.Value = True
Q1H.Value = True
Q1I.Value = True
Q1J.Value = True
End If
End Sub
Private Sub Q1F_Click()
If Q1F.Value = True Then
Q1G.Value = True
ElseIf Q1F.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1G_Click()
If Q1G.Value = False Then
Q1F.Value = False
ElseIf Q1G.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1H_Click()
If Q1H.Value = True Then
Q1I.Value = True
Q1J.Value = True
ElseIf Q1H.Value = False Then
Q1E.Value = False
End If
End Sub
Private Sub Q1I_Click()
If Q1I.Value = True Then
Q1J.Value = True
ElseIf Q1I.Value = False Then
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q1J_Click()
If Q1J.Value = False Then
Q1I.Value = False
Q1H.Value = False
Q1E.Value = False
End If
End Sub
Private Sub Q2A_Click()
If Q2A.Value = True Then
Q2B.Value = True
Q2C.Value = True
End If
End Sub
Private Sub Q2B_Click()
If Q2B.Value = True Then
Q2C.Value = True
ElseIf Q2B.Value = False Then
Q2A.Value = False
End If
End Sub
Private Sub Q2C_Click()
If Q2C.Value = False Then
Q2B.Value = False
Q2A.Value = False
End If
End Sub

Related

Word VBA macro working on some computers but not other

I've written a pretty simple Word macro to hide different parts of a form based on a checkbox made at the beginning of the form. It's only working on some people's computers but not others - it's uploaded to our document server and then users download it out.
Specifically, affected users are able to click a checkbox and the macro will disable the other checkboxes, but the bookmarks remain visible. No error shows up, it just doesn't happen.
The file is downloaded correctly (.docm) and when I poke around in affected users' VBA code, nothing seems to be amiss. I haven't been able to replicate the error myself.
Below is the macro. Any help would be appreciated, as this supports a fairly important business process.
'Plan
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Plan_And_Add").Range.Font.Hidden = CheckBox1.Value
End Sub
'Plan Addendum
Private Sub CheckBox2_Click()
If CheckBox1.Enabled = True Then
CheckBox1.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Plan_And_Add").Range.Font.Hidden = CheckBox2.Value
End Sub
'Execution
Private Sub CheckBox3_Click()
If CheckBox2.Enabled = True Then
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Execution").Range.Font.Hidden = CheckBox3.Value
End Sub
'Extension
Private Sub CheckBox4_Click()
If CheckBox3.Enabled = True Then
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox5.Enabled = True
End If
Bookmarks("CAPA_Extension").Range.Font.Hidden = CheckBox4.Value
Bookmarks("CAPA_Extension_2").Range.Font.Hidden = CheckBox4.Value
End Sub
'Cancellation
Private Sub CheckBox5_Click()
If CheckBox4.Enabled = True Then
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
Else:
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
End If
Bookmarks("CAPA_Cancellation").Range.Font.Hidden = CheckBox5.Value
Bookmarks("CAPA_Cancellation_2").Range.Font.Hidden = CheckBox5.Value
End Sub
'Effectiveness Check Yes
Private Sub CheckBox6_Click()
If CheckBox7.Enabled = True Then
CheckBox7.Enabled = False
Else:
CheckBox7.Enabled = True
End If
End Sub
'Effectiveness Check No
Private Sub CheckBox7_Click()
If CheckBox6.Enabled = True Then
CheckBox6.Enabled = False
Else:
CheckBox6.Enabled = True
End If
Bookmarks("Effectiveness_Check").Range.Font.Hidden = CheckBox7.Value
End Sub
Private Sub CheckBox9_Click()
End Sub
There are a few things wrong with this code logical & syntax wise.
Logical
Private Sub CheckBox1_Click()
If CheckBox2.Enabled = True Then
It doesn't make any sense to me why you are basing what checkboxes are Enabled based on a different checkbox than the one which was clicked; specifically without regard to the state of the checkbox that was clicked. This looks like an ugly work around for not understanding how to initialize and properly control event clicks.
Syntactically
An If Statement looks like this:
If condition Then
' Do something if true
End If
An If-Else statement is formatted like this:
If condition Then
' Do something if true
Else
' Do something else
End if
Labels, used in {On Error} Goto Label statements are formatted with a colon after a name
On Error GoTo ErrorHandler
' Some code that might produce an error
ErrorHandler:
' More code (to deal with errors)
So what you have is a normal If-Statement (without an Else component, since it has a colon after it Else:) which should enable all checkboxes.
If CheckBox2.Enabled = True Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
Else:
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
End If
[TL;DR]
Just remove the colon after the Else statements

Add a second bookmark to the same yes no button

I've created a yes no button that hides a bookmark, how can i add a second bookmark to the same yes no button. My Bookmarks name is TextToShow. i just want to add another bookmark to the same field.
Private Sub CheckBoxNo_Change()
Call ShowHideBookmark
End Sub
Sub ShowHideBookmark()
Dim Sterilisation As Range
Set Sterilisation = ActiveDocument.Bookmarks("TextToShow").Range
If CheckBoxNo.Value = True Then
With Sterilisation.Font
.Hidden = True
End With
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
With Sterilisation.Font
.Hidden = False
End With
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
End If
End Sub
Sub ShowHideBookmark()
Dim Sterilisation As Range, Sterilisation2 As Range
Set Sterilisation = ActiveDocument.Bookmarks("TextToShow").Range
Set Sterilisation2 = ActiveDocument.Bookmarks("TextToShow2").Range
If CheckBoxNo.value = True Then
Sterilisation.Font.Hidden = True
Sterilisation2.Font.Hidden = True
With ActiveWindow.View
.ShowHiddenText = False
.ShowAll = False
End With
Else
Sterilisation.Font.Hidden = False
Sterilisation2.Font.Hidden = False
With ActiveWindow.View
.ShowHiddenText = True
.ShowAll = True
End With
End If
End Sub
Assuming (as per JK's approach) your second bookmarked range is named 'TextToShow2', try:
Private Sub CheckBoxNo_Change()
Application.ScreenUpdating = False
Dim bVal As Boolean: bVal = CheckBoxNo.Value
ActiveDocument.Bookmarks("TextToShow").Range.Font.Hidden = bVal
ActiveDocument.Bookmarks("TextToShow2").Range.Font.Hidden = bVal
ActiveWindow.View.ShowHiddenText = Not bVal
ActiveWindow.View.ShowAll = Not bVal
Application.ScreenUpdating = True
End Sub

Unselect an entire optionbutton group if another optionbutton outside the group is selected

I have optionbutton1 and optionbutton2.
I also have a 8 others optionbuttons grouped under the groupname "category".
I want to deselect any optionbutton from the groupname "category" If OptionButton1.Value = True Or Optionbutton2.Value = True.
I tried this, but it's doesn't work and it's way too long. There must be another way
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Or OptionButton2.Value = True Then OptionButton4.Value = False And OptionButton5.Value = False And OptionButton6.Value = False And _
OptionButton7.Value = False And OptionButton8.Value = False And OptionButton9.Value = False And OptionButton10.Value = False And OptionButton11.Value = False
End Sub
First, there's no need for your If statement, since the first condition will always evaluate to True when OptionButton1 is clicked. Secondly, each statement that sets an option button to False should be on a separate line, and without the And operator. Try the following instead...
Private Sub OptionButton1_Click()
OptionButton4.Value = False
OptionButton5.Value = False
OptionButton6.Value = False
OptionButton7.Value = False
OptionButton8.Value = False
OptionButton9.Value = False
OptionButton10.Value = False
OptionButton11.Value = False
End Sub
However, here's an alternative...
Private Sub OptionButton1_Click()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.GroupName = "Category" Then
ctrl.Value = False
End If
End If
Next ctrl
End Sub
Actually, since you want to reset the "Category" option buttons when OptionButton1 or OptionButton2 is clicked, try the following instead...
Private Sub OptionButton1_Click()
Call ResetCategoryOptionButtons
End Sub
Private Sub OptionButton2_Click()
Call ResetCategoryOptionButtons
End Sub
Private Sub ResetCategoryOptionButtons()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.GroupName = "Category" Then
ctrl.Value = False
End If
End If
Next ctrl
End Sub
Hope this helps!

if else Condition in vb.net

i want the user select duration before he/she continue click the checkbox(Cagayan) how to do that?
this is my code
Private Sub Rcagayan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cagayan.CheckedChanged
If Duration.Text = "" Then
MessageBox.Show("Please Select Duration", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End if
If Cagayan.Checked = False Then
Abulug.Checked = False
Allacapan.Checked = False
Alcala.Checked = False
Amulung.Checked = False
Aparri.Checked = False
Baggao.Checked = False
Ballesteros.Checked = False
Buguey.Checked = False
Camal.Checked = False
Claveria.Checked = False
Enrile.Checked = False
Gattaran.Checked = False
Gonzaga.Checked = False
Iguig.Checked = False
Lasam.Checked = False
Lallo.Checked = False
Pamplona.Checked = False
Penablanca.Checked = False
Piat.Checked = False
Rizal.Checked = False
SantaAna.Checked = False
StaTere.Checked = False
Santonino.Checked = False
Praxedes.Checked = False
Sanchez.Checked = False
Solana.Checked = False
Tuao.Checked = False
Tugue.Checked = False
btnUncheckedckbExpectedFalse.PerformClick()
btnAdvicedUnchecked.PerformClick()
Me.btncagayan.PerformClick()
Me.btnTstmFormat.PerformClick()
End If
End Sub
You have to add exit sub at the end of your first if block...
Private Sub Rcagayan_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cagayan.CheckedChanged
If Duration.Text = "" Then
MessageBox.Show("Please Select Duration", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
exit sub
End if
If Cagayan.Checked = False Then
Abulug.Checked = False
Allacapan.Checked = False
Alcala.Checked = False
Amulung.Checked = False
Aparri.Checked = False
Baggao.Checked = False
Ballesteros.Checked = False
Buguey.Checked = False
Camal.Checked = False
Claveria.Checked = False
Enrile.Checked = False
Gattaran.Checked = False
Gonzaga.Checked = False
Iguig.Checked = False
Lasam.Checked = False
Lallo.Checked = False
Pamplona.Checked = False
Penablanca.Checked = False
Piat.Checked = False
Rizal.Checked = False
SantaAna.Checked = False
StaTere.Checked = False
Santonino.Checked = False
Praxedes.Checked = False
Sanchez.Checked = False
Solana.Checked = False
Tuao.Checked = False
Tugue.Checked = False
btnUncheckedckbExpectedFalse.PerformClick()
btnAdvicedUnchecked.PerformClick()
Me.btncagayan.PerformClick()
Me.btnTstmFormat.PerformClick()
End If
End Sub

VB ComboBox Requires 2 Mouse Clicks to Select Index?

For some reason with this combobox I have in my code, when I run the program, it does not change the Selected Index with one mouse click. Instead, I have to select the drop down of the combo box again, and then select the index. Any ideas on what is going on here? Here is my code, I've tinkered with it for hours.
Public Class Form1
'Mod-level variable
Dim TransactionList As New List(Of Transaction)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Mike Smith's Bank Account"
Lb1.Visible = False
Lb2.Visible = False
Lb4.Visible = False
Tb1.Visible = False
Tb2.Visible = False
SubmitTransactionButton.Visible = False
TransactionListBox.Visible = False
End Sub
Private Sub Cb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Cb1.SelectedIndexChanged
If Tb1.Text = "" Then
Tb1.Text = "0.00"
ElseIf Cb1.Text = "" Then
Lb4.Visible = False
LbAction.Visible = False
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
TransactionListBox.Visible = False
SubmitTransactionButton.Visible = False
ElseIf Cb1.Text = "Credit" Then
Lb4.Visible = False
LbAction.Visible = False
Lb1.Visible = True
Lb2.Visible = True
LbAction.Visible = False
Tb1.Visible = True
Tb2.Visible = True
TransactionListBox.Visible = False
Lb1.Text = "Enter Credit Amount"
Lb2.Text = "Describe the Income"
SubmitTransactionButton.Visible = True
ElseIf Cb1.Text = "Debit" Then
LbAction.Visible = False
Lb1.Visible = True
Lb4.Visible = False
Lb2.Visible = True
LbAction.Visible = False
Tb1.Visible = True
Tb2.Visible = True
TransactionListBox.Visible = False
Lb1.Text = "Enter Debit Amount"
Lb2.Text = "Describe the Expense"
SubmitTransactionButton.Visible = True
ElseIf Cb1.Text = "Display Transactions" Then
TransactionListBox.Visible = True
Lb4.Visible = False
LbAction.Visible = False
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
SubmitTransactionButton.Visible = False
For Each transaction In TransactionList
TransactionListBox.Items.Add(
String.Format("{0}: {1:C}. {2}",
transaction.Type,
transaction.Amount,
transaction.Description))
Next
ElseIf Cb1.Text = "Display Balance" Then
Lb4.Visible = True
Lb1.Visible = False
Lb2.Visible = False
LbAction.Visible = False
Tb1.Visible = False
Tb2.Visible = False
TransactionListBox.Visible = False
Lb4.Text = "Current Balance: $"
End If
End Sub
Private Sub SubmitTransactionButton_Click(sender As Object, e As EventArgs) Handles SubmitTransactionButton.Click
Dim transactionTypeString As String = Cb1.Text
Dim transactionDescriptionstring As String = Tb2.Text
Dim transactionAmountDecimal As Decimal
If Tb1.Text IsNot "" Then
'Tb1.Text = "0.00"
Tb2.Text = ""
Cb1.Text = ""
Lb1.Visible = False
Lb2.Visible = False
Tb1.Visible = False
Tb2.Visible = False
SubmitTransactionButton.Visible = False
End If
Try
transactionAmountDecimal = Decimal.Parse(Tb1.Text)
TransactionList.Add(New Transaction(
transactionTypeString,
transactionAmountDecimal,
transactionDescriptionstring))
Catch FormatExceptionParameter As FormatException
transactionAmountDecimal = 0D
End Try
End Sub
End Class
Public Class Transaction
Public Sub New(type As String, amount As Decimal, description As String)
Me.Type = type
Me.Amount = amount
Me.Description = description
If Me.Type = "Credit" Then
Me.Amount *= -1
End If
End Sub
Public Property Type As String
Public Property Amount As Decimal
Public Property Description As String
End Class