After clarifying with OP, the problem is this:
- The string is not typed by the user manually, but by a code scanner which "works as a person" typing letter by letter;
- The inserted string might trigger a wrong part of the code without being still finished.
A possible solution coming to my mind is a timer which is waiting for the code scanner to write the code into the box, then running the code. Basically:
Declare a global variable
... to remember if the countdown has started yet or not.
Dim started As Boolean
Write your code into another macro
... add the standard code to a macro which is not related to a Change event:
Private Sub TextBox1_Change_Personal() '<-- not triggered if TextBox1 is changed!
Value = TextBox1.Value
If Value <> "" Then
If (Len(Value) = 7 And Right(Value, 1) = "F") Then
ActiveCell.Value = Value
ActiveSheet.Cells(ActiveCell.Row + 1, 1).Select
TextBox1.Value = ""
TextBox1.Activate
ElseIf (Len(Value) = 6 Or (Len(Value) = 5 And (Right(Value, 2) = "IN" Or Right(Value, 2) = "EM"))) Then
ActiveCell.Value = Value
Selection.Offset(0, 1).Select
TextBox1.Value = ""
TextBox1.Activate
Else
'Do nothing
End If
started = False '<-- reset control variable for next code
End Sub
Activate the macro after 3 seconds
... when the code scanner will start writing into the textbox, you "activate a countdown" (basically, you schedule your personal macro to start in 3 seconds). So, before the 3 seconds run, the code scanner will have finished to write the code and you don't fall into the same mistake.
Private Sub TextBox1_Change()
If started = False Then
Application.OnTime TimeSerial(Hour(Now()), Minute(Now()),Second(Now())+3), "TextBox1_Change_Personal"
started = True '<-- deactivating control variable to avoid re-scheduling each time
End If
End Sub
Of course, the 3 seconds are a mock value; your code scanner might be much faster in writing the value into the box (in that case, decrease to 1 second for example) or slower (in that case, increase to 5 seconds for example).
Final (should work) code
Dim started As Boolean
Private Sub TextBox1_Change()
If started = False Then
Application.OnTime TimeSerial(Hour(Now()), Minute(Now()),Second(Now())+3), "TextBox1_Change_Personal"
started = True '<-- deactivating control variable to avoid re-scheduling each time
End If
End Sub
Private Sub TextBox1_Change_Personal() '<-- not triggered if TextBox1 is changed!
Value = TextBox1.Value
If Value <> "" Then
If (Len(Value) = 7 And Right(Value, 1) = "F") Then
ActiveCell.Value = Value
ActiveSheet.Cells(ActiveCell.Row + 1, 1).Select
TextBox1.Value = ""
TextBox1.Activate
ElseIf (Len(Value) = 6 Or (Len(Value) = 5 And (Right(Value, 2) = "IN" Or Right(Value, 2) = "EM"))) Then
ActiveCell.Value = Value
Selection.Offset(0, 1).Select
TextBox1.Value = ""
TextBox1.Activate
Else
'Do nothing
End If
started = False '<-- reset control variable for next code
End Sub
TextBox1with the values?enteras suffix and handleAfterUpdateevent.