So this is the full subroutine which will check the length of the string inside the DB01 textbox first before grouping them according to the found length. I have included @Artik's suggestion on here but I think the v = Array one is not properly written.
Private Sub DB01_AfterUpdate()
Dim MyString As String
Dim strTmp As String
Dim strChar As String
Dim i As Long
Dim v As Variant
If Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 21 Then
v = Array(Me.DB01.Value)
For i = 0 To UBound(v)
MyString = v(i)
If IsNumeric(MyString) Then
strTmp = Format(MyString, "## ### ### ## #### #### ###")
Debug.Print "Input: " & MyString
Debug.Print "Output: " & strTmp
Else
strChar = Mid(MyString, 7, 1)
If strChar Like "[A-z]" Then
strTmp = Replace(MyString, strChar, 7)
strTmp = Format(strTmp, "## ### ### ## #### #### ## #")
Mid(strTmp, 9, 1) = strChar
Debug.Print "Input: " & MyString
Debug.Print "Output: " & strTmp
End If
End If
Debug.Print
Next i
ElseIf Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 13 Then
Me.DB01.Value = Format(Me.DB01.Value, "00 0000 0000 000")
ElseIf Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 11 Then
Me.DB01.Value = Format(Me.DB01.Value, "00000000000")
ElseIf Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 10 Then
Me.DB01.Value = Format(Me.DB01.Value, "0000000000")
ElseIf Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 9 Then
Me.DB01.Value = Format(Me.DB01.Value, "000000000")
ElseIf Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 8 Then
Me.DB01.Value = Format(Me.DB01.Value, "00000000")
ElseIf Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 7 Then
Me.DB01.Value = Format(Me.DB01.Value, "0000000")
ElseIf Len(Application.WorksheetFunction.Substitute(Me.DB01.Value, " ", "")) = 6 Then
Me.DB01.Value = Format(Me.DB01.Value, "000000")
Else
MsgBox "Supply Number Incorrect", vbCritical, "Error"
Me.DB01.Value = ""
End If
End Sub
Bookmarks