VBA to throw an warning if a column contains a value not on an "acceptable list"

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
The cells in column O can only contain certain values. There is a header in O1 and I'd like to leave that open to changes. So, cells O2:O1000 can be 1, 01, 16, 23, 25, or blank (the "01" would be entered in the sheet as '01 so the zero displays). If they are not one of those values, I would like variable ClassCheck (type Variant) to be set to "N". If there are no cells in column O that are not on that list, I would like ClassCheck to be set to "Y".

I do not want to reference a table of acceptable values--I'd like those values to be in the code.

For example:
RowColumn O
1ColHeader
2
301
423
51
6

<tbody>
</tbody>

Would set ClassCheck to "Y"

RowColumn O
1ColHeader
2
317
423
5001
6

<tbody>
</tbody>

Would set ClassCheck to "N" because O3's value is "17" and O5's value is "001", neither of which are on the acceptable list.

Thank you!

Josh in IN
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
So I kept working at this and came up with something that appears to work. Like all my code, it's not graceful.
Code:
    'Check if Class Override values are not on acceptable list    Dim i As Integer
    Dim ClassCheck As Variant
    ClassCheck = "Y"
    For i = 2 To LRow
        If Cells(i, 15).Value = "" Or Cells(i, 15).Formula = "01" Or Cells(i, 15).Formula = "1" Or Cells(i, 15).Formula = "16" Or Cells(i, 15).Formula = "23" Or Cells(i, 15).Formula = "25" Then
        Else
            ClassCheck = "N"
        End If
    Next i
    
    If ClassCheck = "N" Then
        MsgBox "Class Override column contains a value that is not (blank), 01, 1, 16, 23, or 25. Please correct and rerun macro."
        Exit Sub
    End If
 
Upvote 0
You can add some code where indicated by comments to do whatever you intend when ClassCheck is "Y" or "N".
Code:
Sub ClassCheck()
Dim V, i As Long, j As Long, ClassCheck, Acceptable, ct As Long
Acceptable = Array(1, "01", 16, 23, 25, "")
V = Range("O2:O100").Value
For i = LBound(V, 1) To UBound(V, 1)
    ct = 0
    For j = LBound(Acceptable) To UBound(Acceptable)
        If V(i, 1) <> Acceptable(j) Then
            ct = ct + 1
            If ct = UBound(Acceptable) + 1 Then
                ClassCheck = "N"
                'do something here
                Exit Sub
            End If
        Else
            Exit For
        End If
    Next j
Next i
ClassCheck = "Y"
'do something else here
End Sub
 
Upvote 0
Thanks, JoeMo! That's much more robust than my work around. I appreciate it and I can think of a couple other applications where your code will helpful.
 
Upvote 0
Similar to JoeMo

Code:
Sub compare1()
Dim nm As Variant, c As Range, i As Long, ClassCheck As Variant
nm = Array(1, "01", 16, 23, 25)
With ActiveSheet
    For Each c In .Range("O2", .Cells(Rows.Count, 15).End(xlUp))
        If c <> "" Then
            For i = LBound(nm) To UBound(nm)
                If nm(i) = c.Value Then
                    x = x + 1
                End If
            Next
            If x > 0 Then
                ClassCheck = "Y"
            Else
                ClassCheck = "N"
            End If
        End If
        MsgBox ClassCheck
        x = 0
    Next
End With
End Sub
 
Upvote 0
Thanks, JoeMo! That's much more robust than my work around. I appreciate it and I can think of a couple other applications where your code will helpful.
You are welcome - thanks for the reply.
 
Upvote 0
Thanks, JLG--also helpful and educational to a novice like me!
 
Upvote 0

Forum statistics

Threads
1,215,753
Messages
6,126,674
Members
449,327
Latest member
John4520

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