Macro for finding unwanted cell contents

rhino4eva

Active Member
Joined
Apr 1, 2009
Messages
262
Office Version
  1. 2010
Platform
  1. Windows
I have a excel sheet that has a single column starting in A1 and ending at A96. Each cell in this column should contain a 7 digit number apart from two cells that contain the letters "CMV" and "NEG". i need a vba macro to search form a1 to a96 and alert the user with a message box if any cell contains any text other than "CMV" and "NEG" or is not a 7 dgit number

has anyone any ideas :)
 
Code:
Sub test()
    Dim cell      As Range
    Dim v         As Variant
 
    If WorksheetFunction.CountA(Range("A1:A96")) Then
        For Each cell In Range("A1:A96") _
            .SpecialCells(xlCellTypeConstants, _
                          xlNumbers + xlTextValues + xlLogical + xlErrors)
            v = cell.Value2
            Select Case VarType(v)
                Case vbDouble
                    Select Case v
                        Case Is < 1000000, Is > 9999999, 1234567
                            GoTo Oops
                    End Select
                Case vbString
                    Select Case v
                        Case "CMV", "NEG", "RPT"
                        Case Else
                            GoTo Oops
                    End Select
                Case Else
                    GoTo Oops
            End Select
        Next cell
    End If
    Exit Sub
 
Oops:
    cell.Select
    MsgBox "Baaaad!"
End Sub
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
thanks shg for being so understanding. i am not very good at explaining really.
The last request for help was to detect if "RPT" was included in a string

ie

RPT1234567 = ok becuase RPT was used as a prefix
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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