2023年4月8日 星期六

VBA_針對儲純格中( )內字眼標示顏色

 

針對儲純格中( )內字眼標示顏色,如果同一個儲存格純在兩個(),也會都標示顏色。





Sub ChangeColor()

    Dim cell As Range

    Dim i As Integer, j As Integer

    For Each cell In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)

        If cell.Value <> "" Then

            i = InStr(1, cell.Value, "(")

            j = InStr(1, cell.Value, ")")

            If i > 0 And j > 0 And i < j Then

                cell.Characters(i, j - i + 1).Font.ColorIndex = 5 '修改括號內字元的顏色

                If InStr(j + 1, cell.Value, "(") > 0 Then '判斷是否存在第2個以上的括號

                    k = InStr(j + 1, cell.Value, "(")

                    l = InStr(j + 1, cell.Value, ")")

                    If k > 0 And l > 0 And k < l Then

                        cell.Characters(k, l - k + 1).Font.ColorIndex = 5 '修改第2個以上括號內字元的顏色

                    End If

                End If

            End If

        End If

    Next cell

End Sub




沒有留言:

張貼留言