I currently have it set up so that two checkboxes can not be checked at the same time, if they are then it will pop up an error and set the checkbox unchecked again. However currently it is popping up the message twice. How to fix this?
Private Sub CheckBox1_Click()
If CheckBox2.Value = True Then
MsgBox "Must be sent separate."
CheckBox1.Value = False
End If
End Sub
I think you simply need to change it too : the reason is it is running the check when switching checkbox1
Private Sub CheckBox1_Click()
If CheckBox2.Value = True And CheckBox1.Value = True Then
MsgBox "Must be sent separate."
CheckBox1.Value = False
End If
End Sub
Related
I have a userform with 2 OptionButton choices, and I'm modifying the form (hiding labels and controls, and resizing frame) for the default Option (name = BwaIsNew), but then restoring the full userform when Option #2 (name = BwaIsOld) is selected. (see separate question for background).
When Option #2 is selected I'm calling a fresh userform, and coding the change in value. But this coding of the value dlgInformation.BwaIsOld.Value = True then triggers an event (?) that calls the Sub BwaIsOld_Click() code to run. This then sets up a perpetual loop.
What's the best way to solve this?
Problem code (the one looping) is:
Private Sub BwaIsOld_Click()
Unload Me
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
dlgInformation.Show
End Sub
Update:
Thanks #Tim & #CommonSense. I'm still not quite there yet. What am I doing wrong? Here is the code
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Private Sub BwaIsNew_Click()
Call changeform(280)
End Sub
Private Sub BwaIsOld_Click()
Unload Me
Me.EnableEvents = False
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
Me.EnableEvents = True
dlgInformation.Show
End Sub
You need to actually use that EnableEvents in the rest of your code.
BTW I would choose a different name from the built-in Application.EnableEvents property just for clarity.
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Private Sub BwaIsNew_Click()
'don't respond to events triggered by BwaIsOld_Click
If Me.EnableEvents Then
Call changeform(280)
End If
End Sub
Private Sub BwaIsOld_Click()
Unload Me '<< why do this here?
Me.EnableEvents = False
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
Me.EnableEvents = True
dlgInformation.Show
End Sub
I'm trying to create some cue banners for my user form in Word. I got about half way through before I become stuck. I have it where the cue banner will disappear and clear the textbox once it has focus. If the user types their own text, that will be retained as well.
However, if the user doesn't type anything in the textbox once it has been cleared, I want to replace the cue banner along with its attributes (gray text and italicized). I can't seem to get it to work. Here's the code with everything related to this textbox below. The trouble is with the Leave event I think.
Private Sub UserForm_Initialize()
Me.txbShipToName1.Text = "name"
Me.txbShipToName1.Font.Italic = True
Me.txbShipToName1.ForeColor = &H80000006
End Sub
Private Sub txbShipToName1_Enter()
If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1.Text = "name" Then
txbShipToName1.Font.Italic = False
txbShipToName1.ForeColor = &H80000008
txbShipToName1.Text = ""
End If
End Sub
Private Sub txbShipToName1_Leave()
If Me.ActiveControl Is Not txbShipToName1 And Me.txbShipToName1.Text = "" Then
txbShipToName1.Font.Italic = True
txbShipToName1.ForeColor = &H80000006
txbShipToName1.Text = LCase(txbShipToName1.Text)
txbShipToName1.Text = "name"
End If
End Sub
Private Sub txbShipToName1_Change()
If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1 <> "name" Then
txbShipToName1.Text = UCase(txbShipToName1.Text)
End If
End Sub
I'm putting this here for anyone else who would like to know the solution. After a few days of messing with it, I finally realize I was over complicating things when I was messing with another piece of code. I eliminated the ActiveControl test because this is essentially already built into the Enter and Exit events. Here's the updated and working code.
Private Sub UserForm_Initialize()
'Populates cue banners
Me.txbShipToName1.Text = "Name"
Me.txbShipToName1.Font.Italic = True
Me.txbShipToName1.ForeColor = &H80000011
End Sub
Private Sub txbShipToName1_Enter()
'Removes cue banner upon entering textbox
If Me.txbShipToName1.Text = "Name" Then
txbShipToName1.Font.Italic = False
txbShipToName1.ForeColor = &H80000012
txbShipToName1.Text = ""
End If
End Sub 'txbShipToName1_Enter()
Private Sub txbShipToName1_Change()
'Converts textbox to uppercase
If Me.txbShipToName1.Text <> "Name" Then
txbShipToName1.Text = UCase(txbShipToName1.Text)
End If
End Sub 'txbShipToName1_Change()
Private Sub txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Replaces cue banner upon exiting textbox
If Me.txbShipToName1.Text = "" Then
txbShipToName1.Font.Italic = True
txbShipToName1.ForeColor = &H80000011
txbShipToName1.Text = "Name"
End If
End Sub 'txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
I was trying to emphasis the option button from activeX control when it is selected by user. I decided to show the shadow when it is selected and then hide the shadow when user selects other option button. The first process is working whereas the shadow cannot be removed even though I select other button. My VBA code is shown below:
Private Sub OptionButton1_Click()
OptionButton1.Shadow = False
If OptionButton1.Value = True Then
OptionButton1.Shadow = True
Else
OptionButton1.Shadow = False
End If
End Sub
Can anyone please help me to solve this?
In case of FORMS buttons you can use
Sub RemoveFormsButtonShadows()
'A.Leine 21/10/2015
For Each Button In ActiveSheet.Shapes
Button.Select
Selection.ShapeRange.Shadow.Visible = False
Next Button
End Sub
For that you have to create one sub which needs to be called from all the option buttons that you have. This common sub will simply remove the shadow from all option buttons. Here I am taking the example of 3 option buttons.
Option Explicit
Private Sub OptionButton1_Click()
RemoveShadow
If OptionButton1.Value = True Then _
OptionButton1.Shadow = True
End Sub
Private Sub OptionButton2_Click()
RemoveShadow
If OptionButton2.Value = True Then _
OptionButton2.Shadow = True
End Sub
Private Sub OptionButton3_Click()
RemoveShadow
If OptionButton3.Value = True Then _
OptionButton3.Shadow = True
End Sub
Sub RemoveShadow()
Dim objOpt As OLEObject
With ActiveSheet
For Each objOpt In .OLEObjects
If TypeName(objOpt.Object) = "OptionButton" Then
objOpt.Shadow = False
End If
Next
End With
End Sub
Basically Im a vba programmer and Iam facing a serious problem with the checkbox controls of the VBA.I have to select and deselect the checkboxes as per user selection. I believe this is a general question for vb.net also.
some sample code
sub chk3_Click()
if userform.chk3.value = true then
userform.chk4.value = true
userform.chk2.value = true
end if
end sub
sub chk4_click()
if userform.chk4.value = true then
userform.chk3.value=true
userform.chk1.value=true
end if
end sub
This is the sample code, I have to turn on the other checkboxes based on user selection of the checkboxes.but the problem Im facing is when it executes the statement
userform.chk4.value = true in the sub chk3_click
it is invoking the sub chk4_click sub and again finding the
userform.chk3.value=true in sub chk4_click(), invoking the sub chk3_click
I am unable to understand how to solve it .
I have tried with various events mousedown, mouseup and change in value also after update but none worked.These events are crashing the tool I dont understand why but they are crashing so I just ignored to use those events.
Finally I have used a flag which is defined globally in the workbook and using the if condition I have did it but its looking to bad style of coding. Could anyone help me with these ?
This is what I have done to resolve the problem . It works but I dont think its good style of programming.
dim i as integer
sub ch3_click()
if i = 0 then
i = 1
if userform.chk3.value=true then
userform.chk4.value =true
userform.chk2.value=true
end if
i = 0
end if
end sub
sub chk4_click
if i = 0 then
i = 1
if userform.chk4.value = true then
userform.chk3.value=true
userform.chk1.value=true
end if
i = 0
end if
end sub
Any help greatly appreciated.
That is actually a very valid way to approach the problem, you just need to have a more descriptive name and type for i, such as
Dim InClickEvent As Boolean
Then change your click event code to something like:
if Not InClickEvent then
InClickEvent = True
if userform.chk3.value=true then
userform.chk4.value =true
userform.chk2.value=true
end if
InClickEvent = False
end if
Even better is if your version of VBA supports Try/Finally, which I believe it does, you can make sure the flag is always cleared even if you have errors with the following version of the code:
if Not InClickEvent then
Try
InClickEvent = True
if userform.chk3.value=true then
userform.chk4.value =true
userform.chk2.value=true
end if
Finally
InClickEvent = False
End Try
end if
Take a look at Application.EnableEvents
sub chk3_Click()
Application.EnableEvents =false
if userform.chk3.value = true then
userform.chk4.value = true
userform.chk2.value = true
end if
Application.EnableEvents =true
end sub
sub chk4_click()
Application.EnableEvents =false
if userform.chk4.value = true then
userform.chk3.value=true
userform.chk1.value=true
end if
Application.EnableEvents =true
end sub
I have a Word UserForm (Word 2007) with checkboxes and two command controls - Ok and Cancel.
When the form is activated from the macro menu or from an assigned icon, and I click on the checkboxes nothing happens. When I click on the OK button a message appears telling me that I haven't selected anything! When I click on the Cancel button the form unloads.
The checkboxes consist of the Click event which toggles from checked to uncheck. There's also a SelectAll checkbox. When clicked all the other checkboxes are checked or unchecked. When one of the other checkboxes is unchecked the SelectAll checkbox is also unchecked.
When I activate the form from the VBE everything works.
Here's a sample of what I'm talking about:
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
Private Sub btnCancel_Click()
Unload Me
End Sub
Private Sub btnOK_Click()
If Me.CheckBox2.Value = True And Me.CheckBox3.Value = True Then
MsgBox "All checkboxes are checked"
ElseIf Me.CheckBox2.Value = True Then
MsgBox Me.CheckBox2.Name & " is checked"
ElseIf Me.CheckBox3.Value = True Then
MsgBox Me.CheckBox3.Name & " is checked"
ElseIf Me.CheckBox2.Value = False And Me.CheckBox3.Value = False Then
MsgBox "You haven't selected any checkboxes."
End If
End Sub
Private Sub CheckBox2_Click()
If Me.CheckBox2.Value = True Then
Me.CheckBox2.Value = False
Me.ckbSelectAll.Value = False
Else
Me.CheckBox2.Value = True
End If
End Sub
Private Sub CheckBox3_Click()
If Me.CheckBox3.Value = True Then
Me.CheckBox3.Value = False
Me.ckbSelectAll.Value = False
Else
Me.CheckBox3.Value = True
End If
End Sub
Private Sub ckbSelectAll_Click()
If Me.ckbSelectAll.Value = True Then
Me.ckbSelectAll.Value = False
Else
Me.ckbSelectAll.Value = True
End If
If ckbSelectAll.Value = False Then
Me.CheckBox2.Value = False
Me.CheckBox3.Value = False
Else
Me.CheckBox2.Value = True
Me.CheckBox3.Value = True
End If
End Sub
In your click event handlers, such as
Private Sub CheckBox3_Click()
...
End Sub
you check to see if the checkbox is checked (i.e. the value is true) and if it is, set the value to false. This means that a checkbox will always be unchecked as soon as it is checked (or always checked as soon as it is unchecked), giving the appearance that checkboxes don't work properly.
Here's an example click event handler to start (I haven't written VBA for a long time, but I think the following is fine. Will test now...)
Private Sub CheckBox2_Click()
' If checkbox2 is checked but checkbox3 is not, '
' uncheck the select all checkbox '
If CheckBox2 And Not CheckBox3Then
ckbSelectAll = False
End If
End Sub
If you need any further help or tips, then please leave a comment