Ignoring blanks VBA macro

schellett

New Member
Joined
Aug 12, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, can anyone please advise how I can run this macro to ignore blank cells. Thank you

VBA Code:
Sub ValidatePattern()

    Dim cell As Range, rng As Range
    Dim InvalidCount As Long, x As Long
    Dim blInvalid As Boolean

    x = 5    'Column to Validate

    Set rng = ActiveSheet.UsedRange.Columns(x).Offset(8).Resize(ActiveSheet.UsedRange.Rows.Count - 1, 1)

    For Each cell In rng.Cells
        blInvalid = False
        Select Case True
            Case UCase(cell.Value) Like "[A-Z][A-Z]##[A-Z][A-Z][A-Z]"
            Case UCase(cell.Value) Like "[A-Z]#[A-Z][A-Z][A-Z]"
            Case UCase(cell.Value) Like "[A-Z]##[A-Z][A-Z][A-Z]"
            Case UCase(cell.Value) Like "[A-Z]###[A-Z][A-Z][A-Z]"
            Case Else
                blInvalid = True
                'Highlight Invalid Cell Yellow
                cell.Interior.Color = RGB(255, 255, 0)

                'Add Instance to Invalid Counter
                InvalidCount = InvalidCount + 1
        End Select

    Next cell

    'Were there any invalid patterns found?
    If InvalidCount > 0 Then MsgBox "There were " & InvalidCount & _
     " cells found not following the required pattern!"


End Sub
 
Last edited by a moderator:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug. My signature block below has more details. I have added the tags for you this time. 😊

Try adding the blue lines where shown.

Rich (BB code):
For Each cell In rng.Cells
  If Len(cell.Value) > 0 Then
    blInvalid = False
    Select Case True
        Case UCase(cell.Value) Like "[A-Z][A-Z]##[A-Z][A-Z][A-Z]"
        Case UCase(cell.Value) Like "[A-Z]#[A-Z][A-Z][A-Z]"
        Case UCase(cell.Value) Like "[A-Z]##[A-Z][A-Z][A-Z]"
        Case UCase(cell.Value) Like "[A-Z]###[A-Z][A-Z][A-Z]"
        Case Else
            blInvalid = True
            'Highlight Invalid Cell Yellow
            cell.Interior.Color = RGB(255, 255, 0)

            'Add Instance to Invalid Counter
            InvalidCount = InvalidCount + 1
    End Select
  End If
Next cell
 
Upvote 0
Hi Peter,

Thank you so much! Thank you for the tags also, much appreciated.

Regards
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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