Checking and Unchecking a Checkbox - excel

I am stuck with a simple checkbox on excel vba userform. I have the following code in its Click event:
Private Sub CheckBox1_Click()
Me.Label1.Caption = "Checked"
Me.Label1.ForeColor = vbRed
End Sub
When I click the checkbox1 in userform, the Label's caption changes and forecolor too. But when I uncheck it, the label caption does't disappear. Where is my code wrong or am I firing the click event again?

Try this :
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Label1.Caption = "Checked"
Label1.ForeColor = vbRed
Else
Label1.Caption = "UnChecked"
Label1.ForeColor = vbBlack
End If
End Sub

After you paint it red, it won't turn green unless you buy another pot of paint. Nor will it change the caption it has been given without another instruction. Try this code.
Private Sub CheckBox1_Click()
Dim Cap As String ' = "" at this time
With CheckBox1
If .Value Then Cap = "checked"
Me.Label1.Caption = Cap
Me.Label1.ForeColor = IIf(.Value, vbRed, vbGreen)
End With
End Sub
The code intentionally demonstrates 3 ways by which you might achieve the desired result. The smartest is the one which, at once, uses the least amount of code and is the easiest to read (for you).

Related

Userform - Textbox only accept number

I've been searching for a solution to change apperance of my textbox into red when the entered value is not a number.
Actually, I succeeded with a textbox only but I have 14 textboxes at total. I've tried with the codes below:
Private Sub Justi_num()
Dim i As Integer
For i = 2 To 14
If IsNumeric(Controls("TextBox" & i).Value) Then
Controls("TextBox" & i).BackColor = RGB(255, 255, 255) 'Blanc
Else 'Sinon
Controls("TextBox" & i).BackColor = RGB(247, 205, 201) 'Rouge clair
End If
Next i
End Sub
But it didnt work. Any help? Thanks in advance.
You first need to ensure that your code is called whenever the user enters something. This is done with Event-Routines. Controls (like buttons, checkboxes, textboxes and so on) have different events like change (something was changed), dblClick (there was a double click on the control) and so on.
If you want some code to be executed, you need to put code into that event routine. The name of the event routine is combined by the name of the control and the event name. If the name is not following that rule, the code is not called automatically.
In design mode, go to the form editor and double click onto one of your textboxes: The editor will open the text editor window and create a subroutine with the most common event of that control (for a textbox, that's the Change-event, for a button, it would be the Click-event):
Private Sub TextBox1_Change()
End Sub
You could call your routine Justi_num from this routine:
Private Sub TextBox1_Change()
Justi_num
End Sub
However, you will need to create such an event routine for every of your text boxes, there is no way around that (ok, yes, there is, but it is much too complicated for a beginner).
Private Sub TextBox2_Change()
Justi_num
End Sub
Private Sub TextBox3_Change()
Justi_num
End Sub
and so on...
One thing: Your routine Justi_num handles all textboxes, no matter which textbox was changed. You could change the code so that the event routine passes the information which control was changed and the color changing routine handles only that control:
Private Sub TextBox1_Change()
setTextBoxColor Me.TextBox1
End Sub
Private Sub TextBox2_Change()
setTextBoxColor Me.TextBox2
End Sub
(and so on...)
Private Sub setTextBoxColor(tbox As Control)
If IsNumeric(tbox.Value) Or tbox.Value = "" Then
tbox.BackColor = vbWhite
Else
tbox.BackColor = RGB(247, 205, 201)
End If
End Sub
I'll try to help:
One thing that may fail is that "IsNumeric" gives a boolean result. Try using If IsNumeric(Controls("TextBox" & i).Value) = True Then
It would be something like:
Private Sub Justi_num()
Dim i As Integer
Dim NA As Boolean
NA = False
For i = 2 To 14
NA = IsNumeric(Controls("TextBox" & i).Value)
If NA = True Then
Controls("TextBox" & i).BackColor = RGB(255, 255, 255) 'Blanc
Else 'Sinon
Controls("TextBox" & i).BackColor = RGB(247, 205, 201) 'Rouge clair
End If
Next i
End Sub
Hope it helps!

Using ToggleButton's false .Value to remove the grey color on top when it is clicked

I'm trying to make a ToggleButton look always black but when clicked there's always this grey mask appearing on top (probably for the "clicked" effect) :
I found that with the false .Value of the ToggleButton you can remove it but my problem is now that the userform I'm making appear is appearing again once closed. Is there a way to fix that ?
This is my sub for when you click the button :
Private Sub ToggleButton1_Click()
ToggleButton1.Value = False
ToggleButton1.Caption = "Afficher l'interface"
ToggleButton1.ForeColor = RGB(255, 255, 255)
ToggleButton1.BackColor = RGB(0, 0, 0)
UF_Interface.Show 'Userform I want to show when you click the button
End Sub
Thanks
If I may suggest, if you want to use the ToggleButton,
maybe it's better making a two condition (if true then what, if false then what), such as the image below :
So in the image above, if true then open the other UF - if false then close that UF.
Below is the code in the UF where the ToggleButton reside :
Private Sub UserForm_Initialize()
With ToggleButton1
.Caption = "OPEN UF_Interface"
.BackColor = vbYellow
End With
End Sub
Private Sub ToggleButton1_Click()
With ToggleButton1
If .Value = True Then
.Caption = "CLOSE UF_Interface"
.BackColor = vbGreen
UF_Interface.Show vbModeless
Else
.Caption = "OPEN UF_Interface"
.BackColor = vbYellow
Unload UF_Interface
End If
End With
End Sub
Both userforms (UF_Interface and the one with the toggle button) must be shown in vbModeless state. So you need to make another sub in regular module, something like below :
Sub OpenFormWithToggleButton()
FormWithToggleButton.Show vbModeless
End Sub
With the help of #karma I modified my code this way (since I couldn't change the color of a "normal" Button even though I wanted it's way of working) :
Private Sub ToggleButton1_Click()
With ToggleButton1
If .Value = True Then
.Caption = "Afficher l'interface"
.BackColor = vbBlack
.Value = False
UF_Interface.Show 'Userform I wanted to show
End If
End With
End Sub

How to update Userform contents as code progresses

I am having an issue where I have some vba code that runs different functions. I have built out a userform that keeps the user updated on the progress. This isn't a progress bar. It is simply changing a label text. However, when I run it, nothing changes. The code does not finish until I exit out of the userform. Does anyone know how to fix this? The code is listed below. Thanks!
frmALL.Show
xUpload ("DEV")
frmALL.devProgress.Caption = "Complete!"
frmALL.devProgress.ForeColor = vbGreen
frmALL.qaProgress.Caption = "Uploading"
xUpload ("QA")
frmALL.qaProgress.Caption = "Complete!"
frmALL.qaProgress.ForeColor = vbGreen
frmALL.prodProgress.Caption = "Uploading"
xUpload ("PROD")
frmALL.prodProgress.Caption = "Complete!"
frmALL.prodProgress.ForeColor = vbGreen
frmALL.Header.Caption = "Success!"
devProgress, prodProgress and qaProgress all repersent labels in the userform. When I set the forms showModal=false, then it just appears as a white screen until it's done with the code then shows all "Success". This is an issue because it doesn't show the progress like I thought it would. Does my code run too fast? Thanks in advance!
Will try to give a quick example of the _Change() usage based on your code:
Sub Open_UserForm()
'make sure you have a userform_initialize subroutine for base info
frmALL.Show vbModeless
End Sub
In the code for your userform, you might have:
Private Sub ComboBox1_Change()
'ASSUMES DEVPROGRESS IS TRIGGERED BY COMBOBOX1 VALUE CHANGE
xUpload ("DEV")
frmALL.devProgress.Caption = "Complete!"
frmALL.devProgress.ForeColor = vbGreen
frmALL.qaProgress.Caption = "Uploading"
End Sub
Example images... background code for my exmaple:
Private Sub UserForm_Initialize()
ComboBox1.List = Array("cat", "dog")
End Sub
Private Sub combobox1_change()
Label1.Caption = "label 1 = updated"
End Sub
Userform from beginning (pressed F5 from IDE to launch):
Me selecting something in combobox 1:
Combobox1 value changed, label updated:

VBA disable Checkbox click event from firing inside Textbox change event

I want to do something like this. Don't know if it is possible in Excel VBA. On a user form, I have a checkbox and a text box.
When I check the checkbox, the checkbox's caption will be inserted into the text box, when I uncheck the checkbox, the checkbox's caption will be removed from the same text box.
When i write into the text box the caption of the checkbox, inside the text box change event, the checkbox status will be updated. And at the same moment, the checkbox click event is fired and the caption text will be doubled. How can I stop the checkbox click event from firing twice?
Checkbox's click event code:
Private Sub cbSelect_Click()
With TextBox1
.Value = IIf(cbSelect.Value, _
.Value & cbSelect.Caption, _
Replace(.Value, cbSelect.Caption, vbNullString))
End With
End Sub
Text box's change event code:
Private Sub TextBox1_Change()
If InStr(TextBox1.Value, cbSelect.Caption) Then
cbSelect.Value = 1
Else
cbSelect.Value = 0
End If
End Sub
Any idea?
When I tested your code, the checkbox status did not change which I believe is due to the use of binary values (1 / 0) instead of Boolean (True / False)
I changed your Textbox1_Changecode to the following and as soon as I delete part of the checkbox c
Private Sub TextBox1_Change()
If InStr(TextBox1.Value, cbSelect.Caption) Then
cbSelect.Value = True
Else
cbSelect.Value = False
End If
End Sub
I added a Public TextBoxFlag As Boolean at the User_Form module level, and this "flag" is being modifed and checked in both Control's events.
Once the code enters the TextBox1_Change event the flag gets a True value, and when the code reaches the cbSelect_Click event, it doesn't run the code inside if the value of TextBoxFlag = True, only if it's False (didn't came from the TextBox1_Change event, but directly from the CheckBox1 event).
Code
Option Explicit
Public TextBoxFlag As Boolean
Private Sub cbSelect_Click()
' check if event came from the value being modified in the TextBox1_Change event
If TextBoxFlag = False Then
With TextBox1
.Value = IIf(cbSelect.Value, _
.Value & cbSelect.Caption, _
Replace(.Value, cbSelect.Caption, vbNullString))
End With
End If
End Sub
Private Sub TextBox1_Change()
' raise the flag >> came from TextBox change event
TextBoxFlag = True
If InStr(TextBox1.Value, cbSelect.Caption) > 0 Then
cbSelect.Value = True
Else
cbSelect.Value = False
End If
' reset the flag
TextBoxFlag = False
End Sub
your code actually worked for me without the issue you described just substituting:
InStr(Me.TextBox1.Value, Me.cbSelect.Caption)
with :
InStr(Me.TextBox1.Value, Me.cbSelect.Caption) > 0
BTW here's some possibly useful coding advices :
I'd always use Me keyword and refer to Userform controls.
this will also have Intellisense help you in finding UserForm controls names
your cbSelect.Value setting code can be shortened to one line
as a result of all what above:
Private Sub TextBox1_Change()
Me.cbSelect= InStr(Me.TextBox1.Value, Me.cbSelect.Caption) > 0
End Sub
Private Sub cbSelect_Click()
With Me.TextBox1
.Value = IIf(Me.cbSelect, _
.Value & Me.cbSelect.Caption, _
Replace(.Value, Me.cbSelect.Caption, vbNullString))
End With
End Sub

Excel Userform Textbox Behavior

I have a textbox on a userform. It is the only textbox on the form. There are three labels and two buttons in addition to this textbox. Basically, I want the focus to remain on this textbox under all scenarios, other than the moment that one of the buttons would be clicked, but then I want the focus to come right back to the text box. Both buttons have "TakeFocusOnClick" and "TabStop" set to False. I was having problems with getting the focus set to the textbox, which is why I changed these two settings.
Once I changed these settings, the Enter key in the textbox stopped having any effect. I have events written for _AfterUpdate and _KeyPress for the textbox, but they don't fire. As you can see in the code, I have commented out the lines to set the focus to this textbox. Since it is now the only object that can take focus, these lines are not needed (theoretically). When I allowed the other objects to take focus, these lines weren't having any effect (focus was switching to the buttons despite these SetFocus lines).
Here is the code. It is very simple, except that the Enter key isn't triggering the event. Can anyone see why? Thanks.
Private Sub btnDone_Click()
Application.Calculation = xlCalculationAutomatic
formMath.Hide
'Clear statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
End Sub
Private Sub btnSubmit_Click()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_AfterUpdate()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
recordAnswer
End If
End Sub
Private Sub UserForm_Initialize()
'Initialize manual calculation
Application.Calculation = xlCalculationManual
Application.Calculate
'Initialize statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
'Initialize first problem
newProblem
End Sub
Sub recordAnswer()
'Update statistics
Dim attempts, correct As Integer
attempts = Range("attempts").Value
correct = Range("correct").Value
Range("results").Offset(attempts, 0).Value = attempts + 1
Range("results").Offset(attempts, 1).Value = lblTopNum.Caption
Range("results").Offset(attempts, 2).Value = lblBotNum.Caption
Range("results").Offset(attempts, 3).Value = lblBop.Caption
Range("results").Offset(attempts, 4).Value = Range("Answer").Value
Range("results").Offset(attempts, 5).Value = txtAnswer.Text
If (Range("Answer").Value = txtAnswer.Text) Then
Range("results").Offset(attempts, 6).Value = 1
Else
Range("results").Offset(attempts, 6).Value = 0
End If
'Update attempts and success
Range("attempts").Value = attempts + 1
Range("correct").Value = correct + 1
newProblem
End Sub
Sub newProblem()
Application.Calculate
formMath.lblTopNum.Caption = Range("TopNum").Value
formMath.lblBotNum.Caption = Range("BotNum").Value
formMath.lblBop.Caption = Range("ProbType").Value
formMath.txtAnswer.Value = ""
'formMath.txtAnswer.SetFocus
End Sub
To start off
You can either in the design mode, set the TabIndex property of the Textbox to 0 or you can set the focus on the textbox in the UserForm_Initialize()
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Similarly after any operation that you perform, simply call the TextBox1.SetFocus to revert to the textbox.
Option Explicit
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Private Sub CommandButton1_Click()
MsgBox "Hello from Button 1"
TextBox1.SetFocus
End Sub
Private Sub CommandButton2_Click()
MsgBox "Hello from Button 2"
TextBox1.SetFocus
End Sub
Let me know if this is not what you want?
I found a way to accomplish this. In the code above I took out the _KeyPress and _AfterUpdate events and replaced them with:
Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13: recordAnswer
End Select
End Sub
Not sure why the other methods didn't work, but this does.
Also not sure why simply setting the focus directly didn't work. I suspect that the focus was being set, but then something else was happening subsequently that was changing the focus off of the textbox. Just a guess.
Thanks for the help. I appreciate it.

Resources