Userform Textbox SetFocus issue

dreid1011

Well-known Member
Joined
Jun 4, 2015
Messages
3,614
Office Version
  1. 365
Platform
  1. 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:

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.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try moving Cancel = True to inside the If statement.

Hmm, that worked. I was not expecting it to, as 99% of the time the If statement is returning true, so either way I figured the Cancel = True would run in or out of the If statement. Why would it behave differently being in the If statement vs out of it?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top