letters and numbers as a license plate

  • i have the following code

    i need a line to add a random dash as u know some states have 2 letters and numbers some have all letters. Some states have 3 letters and 4 numbers any help would be appreciated Thank You.

  • Code Tags Added
    Your post does not comply with our Forum RULES. Use code tags around code.

    Posting code between


    tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window.

    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)

  • Hi,

    Hope the following meets your requirement. It introduces a "-" at a random position (2nd to 1 less than the length of the string) in the string.

    You can tweak the position of the dash if you have some other criteria for fixing its position.

  • The Coding Assistants provided by B Rama Krishna was great and is really appreciated i am enclosing a copy of the excel project with some license plate examples. while the dash is random it dose not comply with some state formats like Connecticut Texas Canada Pennsylvania. New Jersey. I can not figure it out. More help is needed i am starting to think what are asking is even possible.

    Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range, Cell As Range, strFormat As String Dim i As IntegerOn Error GoTo ErrorHandler:If Not Intersect(Target, Range("C35:R57")) Is Nothing And Target.Value <> "" Then 'change the range as required Application.EnableEvents = False For Each Cell In Target With Cell strFormat = "@@@@" & String(Len(.Value) - 2, "@") .Value = UCase(Format(.Value, strFormat)) i = WorksheetFunction.RandBetween(2, Len(.Value) - 1) .Value = Left(.Value, i) & "-" & Right(.Value, Len(.Value) - i) End With Next Cell Application.EnableEvents = TrueEnd IfExit SubErrorHandler:MsgBox "Error Number:" & Err.Number & vbCrLf & _ "Error Description:" & Err.Description & vbCrLf & _ "Error at: " & Cell.Address Application.EnableEvents = TrueEnd Sub