dreid1011
Well-known Member
- Joined
- Jun 4, 2015
- Messages
- 3,605
- Office Version
- 365
- Platform
- Windows
Good evening. I am writing some code for a userform at work, and I have stumbled upon what seems to be a common problem.
I am trying to set focus on a textbox after a value is entered, so that the user can enter a value, press Enter, and immediately enter another value in the same box and keep going without having to manually select the box each time. The problem is, as I understand it, is that there a chain of events that happen and at the end of this chain the textbox loses focus.
Using TextBox1.SetFocus does not help as it is executed before the end of the chain of events. Here is the code I have currently:
The red arrows indicate the point at which I would like to refocus the individual text boxes. As it is, using "Cancel = True" keeps the focus on whichever text box I start in, and I cannot switch to any other userform element. I have also tried to use 'SendKeys "+{TAB}"', but that did not play nice with 3 _Exit subs.
Any help is much appreciated.
I am trying to set focus on a textbox after a value is entered, so that the user can enter a value, press Enter, and immediately enter another value in the same box and keep going without having to manually select the box each time. The problem is, as I understand it, is that there a chain of events that happen and at the end of this chain the textbox loses focus.
Using TextBox1.SetFocus does not help as it is executed before the end of the chain of events. Here is the code I have currently:
Rich (BB code):
Option Explicit
Private Sub UserForm_Initialize()
ChecksBoxIn.Value = ""
CChecksBoxIn.Value = ""
MOrdersBoxIn.Value = ""
ChecksList.Clear
CChecksList.Clear
MOrdersList.Clear
ChecksTotalBox.Value = 0
CChecksTotalBox.Value = 0
MOrdersTotalBox.Value = 0
TotalBox.Value = 0
ChecksBoxIn.SetFocus
End Sub
Private Sub ChecksBoxIn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(ChecksBoxIn.Value) Then
ChecksList.AddItem Format(ChecksBoxIn.Value, "$#,##0.00") & " "
End If
ChecksBoxIn.Value = ""
Call CheckTotal
'Cancel = True <---------------------------
End Sub
Private Sub CChecksBoxIn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(CChecksBoxIn.Value) Then
CChecksList.AddItem Format(CChecksBoxIn.Value, "$#,##0.00") & " "
End If
CChecksBoxIn.Value = ""
Call CheckTotal
'Cancel = True <---------------------------
End Sub
Private Sub MOrdersBoxIn_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(MOrdersBoxIn.Value) Then
MOrdersList.AddItem Format(MOrdersBoxIn.Value, "$#,##0.00") & " "
End If
MOrdersBoxIn.Value = ""
Call CheckTotal
'Cancel = True <---------------------------
End Sub
Private Sub CheckTotal()
Dim i As Long, j As Long, k As Long
Dim CValue As Double, CCValue As Double, MOValue As Double
For i = 0 To ChecksList.ListCount - 1
CValue = CValue + CDbl(ChecksList.List(i))
Next i
ChecksTotalBox.Value = CValue
ChecksTotalBox = Format(ChecksTotalBox, "$#,##0.00")
For j = 0 To CChecksList.ListCount - 1
CCValue = CCValue + CDbl(CChecksList.List(j))
Next j
CChecksTotalBox.Value = CCValue
CChecksTotalBox = Format(CChecksTotalBox, "$#,##0.00")
For k = 0 To MOrdersList.ListCount - 1
MOValue = MOValue + CDbl(MOrdersList.List(k))
Next k
MOrdersTotalBox.Value = MOValue
MOrdersTotalBox = Format(MOrdersTotalBox, "$#,##0.00")
TotalBox.Value = CDbl(ChecksTotalBox.Value) + CDbl(CChecksTotalBox.Value) + CDbl(MOrdersTotalBox.Value)
TotalBox = Format(TotalBox, "$#,##0.00")
End Sub
The red arrows indicate the point at which I would like to refocus the individual text boxes. As it is, using "Cancel = True" keeps the focus on whichever text box I start in, and I cannot switch to any other userform element. I have also tried to use 'SendKeys "+{TAB}"', but that did not play nice with 3 _Exit subs.
Any help is much appreciated.