VBA to find the font color of a string

后端 未结 2 1483
执笔经年
执笔经年 2020-12-19 18:18

I am new to VBA..I am writing a macro for some file comparison..My requirement is if a string has red color font,that string should be ignored for iteration and code should

相关标签:
2条回答
  • 2020-12-19 19:00

    May I assume that the source for your data is an excel-sheet (as pointed out by previous comments, a pure string does not hold information on colours), and for one reason or another you want to use an array. Then this might be a way to solve the problem (prerequisite: full string is in one colour only)

    (Just saw that there's a solution provided by Gary's student without using arrays as well...AND providing for cases where only part of the string is red...nice one!)

    Sub colour()
    
    Dim arr_DB As Variant
    Dim i As Long
    
    ReDim arr_DB(1, 1)  'Array size to be adjusted as needed, Base 0 !
    
    For i = 1 To 2
        arr_DB(i - 1, 0) = ActiveSheet.Cells(i, 1).Value   'Value of Cell
        arr_DB(i - 1, 1) = ActiveSheet.Cells(i, 1).Font.Color  'Colour of Font in Cell
    
    Next
    
    If arr_DB(i - 1, 1) = 255 Then   ' No. 255 is colour RED
    
        'skip.....
    
    End If
    
    End Sub

    0 讨论(0)
  • 2020-12-19 19:02

    Say we have cells A1 thru A4 like:

    pic

    Then this code will find the non-red characters:

    Sub ColorTest()
        Dim I As Long, J As Long
        For I = 1 To 4
            For J = 1 To Len(Cells(I, 1).Value)
                If Cells(I, 1).Characters(Start:=J, Length:=1).Font.Color <> vbRed Then
                    MsgBox "non-red found at cell A" & I & " position " & J
                End If
            Next J
        Next I
    End Sub
    
    0 讨论(0)
提交回复
热议问题