VBA Multiple Textboxes Validation control - excel

Dears,
I am preparing a simple userform to let a teacher to enter each student’s name and height in a class. There are two textboxes (textbox_name and textbox_height) and one “add to record” command button. I tried to make some validation control to prevent empty string to be entered. My validation control is in case textbox is empty, when the click the “add to record” button, the corresponding empty textbox turns pink; and when both textboxes are empty, both textboxes should turn pink, however only the first textbox turns pink in my below codes. Can you tell me why and how to correct it? Thanks.
Private Sub Cmd_addtorecord_Click()
If TextBox_Name = "" Then
TextBox_Name.BackColor = rgbPink
Exit Sub
End If
If TextBox_height = "" Then
TextBox_height.BackColor = rgbPink
Exit Sub
End If
'....
End sub

You can do it like this:
Private Sub Cmd_addtorecord_Click()
'ensure required fields have content
If FlagEmpty(Array(TextBox_Name, TextBox_height)) > 0 Then
MsgBox "One or more required values are missing", vbExclamation
Exit Sub
End If
'....
End Sub
'check required textboxes for content, and flag if empty
' return number of empty textboxes
Function FlagEmpty(arrControls) As Long
Dim i As Long, numEmpty As Long, clr As Long
For i = LBound(arrControls) To UBound(arrControls)
With arrControls(i)
clr = IIf(Len(Trim(.Value)) > 0, vbWhite, rgbPink)
.BackColor = clr
If clr = rngpink Then numEmpty = numEmpty + 1
End With
Loop
FlagEmpty = numEmpty
End Function

You're Exiting the sub before you get to the second textbox. Change your code to something like this:
Private Sub Cmd_addtorecord_Click()
If TextBox_Name = "" or TextBox_height = "" Then
If TextBox_Name = "" Then
TextBox_Name.BackColor = rgbPink
End If
If TextBox_height = "" Then
TextBox_height.BackColor = rgbPink
End If
Exit Sub
End If
'....
End sub

Related

Pre-Populate a Multi Select Listbox in Excel VBA

Why doesn't this code open the form with the items selected already? I set the selection flag to true with this code.
Private Sub UserForm_Initialize()
Dim i, InStrRes, k
With ActiveCell
If .Value <> "" Then
For i = 0 To Me.lstDV.ListCount - 1
InStrRes = InStr(1, ActiveCell.Value, Me.lstDV.List(i))
If InStrRes <> 0 And InStrRes <> Null Then
Me.lstDV.Selected(i) = True
End If
Next i
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim i, InStrRes, k
With ActiveCell
If .Value <> "" Then
For i = 0 To Me.lstDV.ListCount - 1
Me.lstDV.Selected(i) = False '<~~ add this code. Should be set to false in advance.
InStrRes = InStr(1, ActiveCell.Value, Me.lstDV.List(i))
'If InStrRes <> 0 And InStrRes <> Null Then
If InStrRes Then
Me.lstDV.Selected(i) = True
End If
Next i
End If
End With
End Sub
The presumption must be that the listbox hasn't been loaded at the time it is initialized. I tried the Activate event with similar lack of success. Please try this code instead.
Sub ShowMyForm()
Dim MyForm As New TryForm
Dim CellVal As Variant
CellVal = ActiveCell.Value
With MyForm
If Len(CellVal) Then
On Error Resume Next
.lstDV.ListIndex = Application.Match(CellVal, Range(.lstDV.RowSource), 0) - 1
End If
.Show
' code continues here when the form is closed
End With
Unload MyForm
End Sub
This code must be in a standard code module, not the form's code sheet. It calls the form TryForm (replace with the name you gave to your form), and modifies it before showing. Note that you still have full access to the form to take out values you may want after it is hidden. Just avoid Unload Me anywhere in the form's code because the unloading is done by the above code after you have taken everything you wanted.
Note that the form's Initialize event is triggered by the New key word in Dim MyForm As New TryForm, long before you gain access. The Activate event follows with some delay. Please make sure that the procedures you may run with these events don't interfere with what the above code does.

Create a VBA program that will perform an addition or subtraction of two Hexadecimal numbers

I'm trying to get both addition and subtraction options to work and display the proper symbol between each label box. I've been running into an error, which VBA highlights for me. The boxes A, B, and C need to be valued at 10, 11, and 12. When you click a box it's supposed to populate the first empty box with the value. When both boxes are full and you click another letter, its should not be changed, instead the values will be locked until you hit clear.
Current code:
Private Sub Addition_Click()
Me.Result = (Me.LblFirstNum + 0) + (Me.LblSecondNum + 0)
'lblresult = Val(LblFirstNum) + Val(LblSecondNum)
End Sub
Private Sub BtnA_Click()
With LblFirstNum
'to display text
.Caption = "10"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub BtnB_Click()
With LblSecondNum
'to display text
.Caption = "11"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub BtnC_Click()
With LblSecondNum
'to display text
.Caption = "12"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub Calculate_Click()
'Me.Result = (Me.LblFirstNum + 0) + (Me.LblSecondNum + 0)
lblresult.Value = Val(LblFirstNum.Value) + Val(LblSecondNum.Value)
End Sub
Private Sub Clear_Click()
Unload UserForm1
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
'Exit Command
UserForm1.Hide
End Sub
Private Sub Result_Change()
End Sub
Private Sub Label4_Click()
End Sub
Private Sub LblFirstNum_Click()
End Sub
Private Sub LblSecondNum_Click()
End Sub
Private Sub LblSign_Click()
End Sub
Private Sub Subtraction_Click()
'Me.Result = (Me.LblFirstNum + 0) - (Me.LblSecondNum + 0)
lblresult.Value = Val(LblFirstNum.Value) - Val(LblSecondNum.Value)
End Sub
In Calculate_Click() and Subtraction_Click(), you've tried to retrieve the text shown on a label using LblFirstNum.Value and LblSecondNum.Value. Labels don't have a "Value" property. The correct property to use is "Caption".
In Addition_Click(), it's unclear whether Result is the correct name for the control you're trying to reference. Elsewhere in this code, you have lblresult so you should establish which is the correct name and use that throughout.
You may also want to check the use of Unload in Clear_Click() (assuming that UserForm1 is the form we are working with). Your current code will probably result in an automation error on the UserForm1.Show line

After user input, and I have autofilled 2 other textboxes, how do I stop my macro from running a search on the other boxes?

I have 3 textboxes, of which when you input data into any one of the textboxes, the other 2 will populate.
the codes are very long, so I've just added what it does at every line of code
Private Sub tbAC_AfterUpdate()
'Declarations
textbox 1 = tbAC
textbox 2 = tbAN
textbox 3 = tbIC
If tbAC is Nothing Then
tbAC = ""
Elseif tbAC.TextLength > 0 Then
'Find tbAC in column AGC
Set MCodef = wsa.Columns(AGC).Find(what:=tbAC, LookIn:=xlValues, Lookat:=xlPart)
If MCodef is Nothing Then
Msgbox ("Invalid Code")
Else
Mcoder = Mcodef.Row
tbAN = wsa.Range(AGN & Mcoder).Value
tbIC = wsa.Range(AIC & Mcoder).Value
End if
End If
End Sub
'It's pretty much the same if you fill in tbAN or tbIC
Private Sub tbAN_AfterUpdate()
End Sub
Private Sub tbIC_AfterUpdate()
End Sub
Given that user input in tbAN, I only want excel to run the sub tbAN_AfterUpdate and not the others. Any help would be appreciated, thanks in advance!
If you have code that is triggered by entering data into the text box, but don't want that code to execute when using an alternate function in VBA, you can use:
Application.EnableEvents = False
So if you are entering into TextBox1 and want to update TextBox2 when you enter into TextBox1, put this into TextBox1's Code. You would use this when the code is first initialized, but you need to remember to reverse that at the end of the code or prior to the code terminating (e.g. through Exit Sub or some other forced command) as once EnableEvents = False, no code will trigger without setting it to:
Application.EnableEvents = True

Excel VBA: Highlight textbox value after error occurs

I am trying to highlight entered value in TextBox. TextBox value is representing date value in date forma DD-MM-YYYY. I wrote some code to validate if inserted date is ok (in example 31 of April).
Hightlight itself is not a problem, however I want to do this right after an error occurs. So when I insert 31-04-2014, I should get the message "You have inserted wrong date" and the date value should hightlighted. For now it shows me message, highlights value and focus is set to another CommandButton
So far I made something like this:
Private Sub data_faktury_AfterUpdate()
Dim dzien As Byte, miesiac As Byte
Dim rok As Integer
On Error GoTo blad:
dzien = Mid(data_faktury.Value, 1, 2)
miesiac = Mid(data_faktury.Value, 4, 2)
rok = Right(data_faktury.Value, 4)
Call spr_date(dzien, miesiac, rok)
Exit Sub
blad:
If Err.Number = 13 Then
If data_faktury <> "" Then
If Len(data_faktury) < 10 Then: MsgBox ("Źle wpisana data faktury.")
End If
End If
End Sub
And code for 2nd procedure:
Sub zle()
MsgBox ("Wybrałeś zły dzień")
With Faktura.data_faktury
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
This is a bit long for a comment so here goes. The basic principle is to use the exit event and cancel when necessary. To prevent this being fired when you close the form, you need to use a flag variable - example userform code:
Private bSkipEvents As Boolean
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bSkipEvents Then Exit Sub
With TextBox1
If Not IsValidDate(.Text) Then
Cancel = True
MsgBox "Invalid date"
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
bSkipEvents = True
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