如何读取所有数据单元格并仅突出显示具有10个以上字符的单元格

时间:2019-01-28 21:25:57

标签: excel vba

我需要读取所有包含条目的数据单元格,但是我只需要突出显示该数据单元格中字符大于10的单元格即可。

例如:

在A列中,我需要读取所有数据,但我的条件是我需要突出显示包含10个以上字符的单元格。

同样,在B列中,我需要做同样的事情,但是在这里,我需要突出显示包含12个以上字符的单元格。

同样,我想为包含数据的所有列实现一种解决方案。

请帮助我解决该问题。

我尝试的代码:

Sub Dendrinos2()
    Dim i As Long
    Dim lr As Long

    lr = Cells(Rows.Count, 5).End(xlUp).Row

    For i = lr To 2 Step -1
        If Range("C" & i).Value > 6 Then Range("C" & i).Interior.ColorIndex = 3
        If Range("G" & i).Value > 3 Then Range("G" & i).Interior.ColorIndex = 3
        If Range("I" & i).Value > 3 Then Range("I" & i).Interior.ColorIndex = 3
        If Range("C" & i).Value < -3 Then Range("C" & i).Interior.ColorIndex = 3
        If Range("G" & i).Value < -3 Then Range("G" & i).Interior.ColorIndex = 3
        If Range("I" & i).Value < -3 Then Range("I" & i).Interior.ColorIndex = 3
        If Range("E" & i).Value = "--" Then Range("E" & i).Interior.ColorIndex = Range("A" & i).Interior.ColorIndex
        If Range("G" & i).Value = "--" Then Range("G" & i).Interior.ColorIndex = Range("A" & i).Interior.ColorIndex
        If Range("I" & i).Value = "--" Then Range("I" & i).Interior.ColorIndex = Range("A" & i).Interior.ColorIndex  
    Next i
End Sub

2 个答案:

答案 0 :(得分:0)

使用条件格式和覆盖A和B列的简单公式。

Sub highlightLength()
    With Worksheets("sheet3")
        With .Range("A:B")
            .FormatConditions.Delete
            .FormatConditions.Add Type:=xlExpression, Formula1:="=len(a1)>(column(a1)+4)*2"
            With .FormatConditions(.FormatConditions.Count)
                .Interior.Color = vbYellow
            End With
        End With
    End With
End Sub

答案 1 :(得分:0)

我会做这样的事情:

Sub Dendrinos2()
    Dim i As Long
    Dim lr As Long
    Dim sht As Worksheet

    Set sht = ActiveSheet
    lr = sht.Cells(sht.Rows.Count, 5).End(xlUp).Row

    For i = lr To 2 Step -1
        Checklength sht.Range("A" & i), 10
        Checklength sht.Range("B" & i), 12
        CheckLimits sht.Range("C" & i), -3, 6
        CheckLimits sht.Range("G" & i), -3, 3
        CheckLimits sht.Range("I" & i), -3, 3
        CheckDashes sht.Range("E" & i), sht.Range("A" & i)
        CheckDashes sht.Range("G" & i), sht.Range("A" & i)
        CheckDashes sht.Range("I" & i), sht.Range("A" & i)
    Next i

End Sub

Sub CheckLimits(c As Range, ll, ul)
    With c
        If .Value < ll Or .Value > ul Then .Interior.ColorIndex = 3
    End With
End Sub

Sub CheckDashes(c As Range, cA As Range)
    With c
        If .Value = "--" Then
            .Interior.ColorIndex = cA.Interior.ColorIndex
        End If
    End With
End Sub

Sub Checklength(c As Range, l As Long)
    With c
        If Len(.Value) > l Then .Interior.ColorIndex = 3
    End With
End Sub