ipbr21054
Well-known Member
- Joined
- Nov 16, 2010
- Messages
- 5,744
- Office Version
- 2007
- Platform
- Windows
Evening,
I have a userform where TextBox2 needs to have 17 characters entered.
It must be 17 or show Msgbox.
I am not sure if the code should be on that Textbox so when i leave it i see the Msgbox Or when i press the command button to transfer userform values to worksheet.
I also paste into Textbox 2 should you need to know.
Below i have something which kind of works on the Textbox but as soon as i strart to type 1 letter the msgbox is shown.
What does you advise.
Basically if Textbox2 isnt 17 characters then no values should be transfered to worksheet
Userform code.
I have a userform where TextBox2 needs to have 17 characters entered.
It must be 17 or show Msgbox.
I am not sure if the code should be on that Textbox so when i leave it i see the Msgbox Or when i press the command button to transfer userform values to worksheet.
I also paste into Textbox 2 should you need to know.
Below i have something which kind of works on the Textbox but as soon as i strart to type 1 letter the msgbox is shown.
What does you advise.
Basically if Textbox2 isnt 17 characters then no values should be transfered to worksheet
Code:
Private Sub TextBox2_Change()
If TextBox2.Value <> 17 Then
MsgBox "VIN MUST BE 17 CHARACTERS", vbCritical, "VIN CHARACTER COUNT MESSAGE"
TextBox2.Value = ""
Else
Exit Sub
TextBox2 = UCase(TextBox2)
End If
End Sub
Userform code.
Code:
Private Sub CommandButton1_Click()
Dim i As Integer
Dim X As Long
Dim ControlsArr(1 To 8) As Variant
For i = 1 To 8
If i > 2 Then
With Me.Controls("ComboBox" & i)
If .ListIndex = -1 Then
MsgBox "YOU MUST COMPLETE ALL FIELDS", vbCritical, "MC LIST TRANSFER"
TextBox1.SetFocus
Exit Sub
Else
ControlsArr(i) = .Value
End If
End With
Else
ControlsArr(i) = Me.Controls("TextBox" & i).Value
End If
Next i
Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("MC LIST")
.Range("A8").EntireRow.Insert Shift:=xlDown
.Range("A8:I8").Borders.Weight = xlThin
.Cells(8, 1).Resize(, UBound(ControlsArr)).Value = ControlsArr
End With
Range("B8").Select
Range("A8").Select
ActiveWorkbook.Save
Application.ScreenUpdating = True
MsgBox "Database Has Been Updated", vbInformation, "SUCCESSFUL MESSAGE"
With ThisWorkbook.Worksheets("MC LIST")
If .AutoFilterMode Then .AutoFilterMode = False
X = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A7:I" & X).Sort Key1:=Range("A8"), Order1:=xlAscending, Header:=xlGuess
.Range("B8").Select
.Range("A8").Select
End With
Unload McListForm
End Sub