Search code examples
excelvbaconditional-formatting

Check a column of data for data to be other than integer, and delete the contents of cells that are not integers


I am trying to create a macro in Excel that will delete cells in a specific column if they are not integers, then check the data left for value's greater than .8 and turn the cell red and text white. I can do the 2nd part no problem, but i have mixed data types (text and numbers) and empty cells scattered through out - and cannot figure out how to only remove the contents on the cells with something other than a number in it.

I only want to clear the contents of the cells that have non-number data so i can run the conditional formatting code i already have.

sample data:

Column F
2023
73%
50%
9%
16%
4pm
Sep
?

i need the cells with "4pm", "Sep" & "?" (this data will change) to be emptied. The column's length can vary but would never be more than 200 rows.

this is my current code, but i need it to only work on cells with integers, i need the rest to be either emptied - or better yet - no formatting at all

' Verify diskstats (looking for >80%)
'

'


Dim r As Range, arr, i
    Set r = Range([F1], Cells(Rows.Count, "F").End(xlUp))
    arr = r.Value
    For i = 1 To UBound(arr)
        ' Validate and clean non-numeric cell
        If Not VBA.IsNumeric(arr(i, 1)) Then arr(i, 1) = ""
    Next
    r.Value = arr

    
 'between 1 & 80% turns green
    
     Columns("F:F").Select
    
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=0.00000001", Formula2:="=0.8"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
    End With
    
    'greater than 80% turns red
    
    Columns("F:F").Select
    If Right(Selection.NumberFormat, 1) = "%" Then
    
    
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=0.8"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    End If
    
   ActiveCell.Columns("A:A").EntireColumn.Select
    
    
End Sub

Solution

  • Provide code will clean the non-numeric cells in Column F.

    Sub demo()
        Dim r As Range, arr, i
        Set r = Range([F1], Cells(Rows.Count, "F").End(xlUp))
        arr = r.Value
        For i = 1 To UBound(arr)
            ' Validate and clean non-numeric cell
            If Not VBA.IsNumeric(arr(i, 1)) Then arr(i, 1) = ""
        Next
        r.Value = arr
    End Sub
    

    Updated code to answer the question in comment.

        Dim r As Range
        For Each r In Range([F1], Cells(Rows.Count, "F").End(xlUp))
            If Not VBA.IsNumeric(r.Value) Then r.Value = ""
            If Right(r.NumberFormat, 1) = "%" Then
                r.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
                    Formula1:="=0.8"
                r.FormatConditions(r.FormatConditions.Count).SetFirstPriority
                With r.FormatConditions(1).Font
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = 0
                End With
                With r.FormatConditions(1).Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 255
                    .TintAndShade = 0
                End With
                With r.FormatConditions(1).Font
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                End With
            End If
        Next