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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
you can create something that is pattern matched, but you need to demonstrate all the valid patterns before doing that
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

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
Back
Top