Verifying a specific format of the cell value

protrader

New Member
Joined
Oct 1, 2013
Messages
2
Hi, I need to check whether the values in column C have specific format and then execute some commands. The specific format is two or three letters, one dash and 3 numbers, for example:

AB-123
ABC-345
ZZ-001
RE-909

Is there any way to do this in Excel VBA?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to the board.

You could use the Like operator and check it against patterns. For example here is a function that allows you to pass a number of different pattern strings. If it matches any then it returns True.

Code:
Public Function TestPattern(ByVal strValue As String, ParamArray varPattern() As Variant) As Boolean
    Dim varPatterns As Variant
    Dim lngItem As Long
    
    varPatterns = varPattern
    For lngItem = LBound(varPatterns) To UBound(varPatterns)
        TestPattern = strValue Like varPatterns(lngItem)
        If TestPattern = True Then Exit Function
    Next lngItem
End Function

You can call this in your code, e.g:
Code:
Public Sub demo()
    Dim blnMatch As Boolean
    blnMatch = TestPattern(Range("A1").Value, "[A-Z][A-Z]-###", "[A-Z][A-Z][A-Z]-###")
    MsgBox blnMatch
End Sub

Or even use it as a worksheet function, e.g:
=TestPattern(A1,"[A-Z][A-Z]-###","[A-Z][A-Z][A-Z]-###")
 
Upvote 0
protrader,

Welcome to the MrExcel forum.

Sample raw data:


Excel 2007
CD
1AB-123
2ABC-345
3ZZ-001
4RE-909
5A-12
6ABCD-123
7ABC-34
8Z-001
9A-1
10AB-123
11ABC-345
12
Sheet1


After the macro:


Excel 2007
CD
1AB-123Format Matches
2ABC-345Format Matches
3ZZ-001Format Matches
4RE-909Format Matches
5A-12No Match
6ABCD-123No Match
7ABC-34No Match
8Z-001No Match
9A-1No Match
10AB-123Format Matches
11ABC-345Format Matches
12
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub CheckFormat()
' hiker95, 10/21/2013
' http://www.mrexcel.com/forum/excel-questions/734062-verifying-specific-format-cell-value.html
Dim c As Range
For Each c In Range("C1", Range("C" & Rows.Count).End(xlUp))
  If c Like "[A-Z][A-Z]-###" Or c Like "[A-Z][A-Z][A-Z]-###" Then
    c.Offset(, 1) = "Format Matches"
  Else
    c.Offset(, 1) = "No Match"
  End If
Next c
Columns("D").AutoFit
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CheckFormat macro.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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