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 :)
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try

Code:
Sub test()
Dim i As Long
For i = 1 To 96
    With Range("A" & i)
        If IsNumeric(.Value) Then
            If Len(.Value) <> 7 Then MsgBox "Row " & i & " numeric <> 7 digits"
        Else
            If .Value <> "CMV" And .Value <> "NEG" Then MsgBox "Row " & i & " not CMV or NEG"
        End If
    End With
Next i
End Sub
 
Upvote 0
thanks for that but a slight problem !!!
it detects "0" in empty cells
 
Upvote 0
You didn't say that cells could be blank :rolleyes:

Code:
Sub test()
Dim i As Long
For i = 1 To 96
    With Range("A" & i).SpecialCells(xlCellTypeConstants)
        If IsNumeric(.Value) Then
            If Len(.Value) <> 7 Then MsgBox "Row " & i & " numeric <> 7 digits"
        Else
            If .Value <> "CMV" And .Value <> "NEG" Then MsgBox "Row " & i & " not CMV or NEG"
        End If
    End With
Next i
End Sub
 
Upvote 0
Maybe ...
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, _
                          lNumbers + xlTextValues + xlLogical + xlErrors)
            v = cell.Value2
            Select Case VarType(v)
                Case vbDouble
                    If v < 1000000 Or v > 9999999 Then GoTo Oops
                Case vbString
                    If v <> "CMV" And v <> "NEG" Then GoTo Oops
                Case Else
                    GoTo Oops
            End Select
        Next cell
    End If
    Exit Sub
 
Oops:
    cell.Select
    MsgBox "Baaaad!"
End Sub
 
Upvote 0
hi shg
tried your code but it does not quite there
i dont understand the line

If v < 1000000 Or v > 9999999 Then GoTo Oops

i want it to goto Oops if number is not 7 digits long .. ihave tried with
1, 10, 100 etc and it still passes
 
Upvote 0
All positive 7-digit numbers are between 1000000 and 9999999, so if it's outside that range, it isn't 7 digits.

EDIT: That's curious. This continuation line ...
Code:
                          lNumbers + xlTextValues + xlLogical + xlErrors)
should be
Code:
                          [COLOR=red]x[/COLOR]lNumbers + xlTextValues + xlLogical + xlErrors)
 
Last edited:
Upvote 0
i know i am pushing my luck shg but if i wanted to exclude a string containing "RPT"and a 7 digit number how would i do that
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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