VBA Code to read particular format

mahiT

New Member
Joined
May 30, 2015
Messages
5
Hello Everyone,

I am new here and its my first post :)

Please help me for a VBA code to read particular format like Indian Vehicle Registration number (Ex- KA 09 AC 1254) when typed in a input box/ textbox it should give a warning like 'Type the correct Number' or 'The Registration number given is not valid'

Waiting for your help.:rolleyes:
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

mole999

Moderator
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
you can create something that is pattern matched, but you need to demonstrate all the valid patterns before doing that
 

mahiT

New Member
Joined
May 30, 2015
Messages
5
you can create something that is pattern matched, but you need to demonstrate all the valid patterns before doing that

But How to do pattern match?? Please give some example so I have an idea and Thanks for the reply
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,981
Office Version
  1. 2016
Platform
  1. Windows
I believe this function will do what you want (I had to read online what an Indian Vehicle Registration number could look like)... just pass the text in your TextBox into it and display its results in a MessageBox (or elsewhere) or include it in an If..Then test (the function returns one of these results... "Valid", "Invalid" or ""Invalid State or Union Territory Code!").
Code:
Function IsRegValid(S As String) As String
  Dim X As Long, Parts() As String
  Const States = ",AN,AP,AR,AS,BR,CG,CH,DD,DL,DN,GA,GJ,HR,HP,JH,JK,KA,KL," & _
                 "LD,MH,ML,MN,MP,MZ,NL,OD,PB,PY,RJ,SK,TN,TR,TS,UK,UP,WB,"
  IsRegValid = "Invalid"
  If S Like "[A-Za-z][A-Za-z] #* ####" Then
    Parts = Split(S)
    If InStr(States, "," & Parts(0) & ",") Then
      If UBound(Parts) > 2 Then
        If Len(Parts(2)) > 0 And Parts(2) Like Replace(String( _
                    Len(Parts(2)), "X"), "X", "[A-Za-z]") Then
          IsRegValid = "Valid"
        End If
      Else
        IsRegValid = "Valid"
      End If
    Else
      IsRegValid = "Invalid State or Union Territory Code!"
    End If
  End If
End Function
 

Watch MrExcel Video

Forum statistics

Threads
1,130,310
Messages
5,641,450
Members
417,210
Latest member
rins

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
Top