excelでセル内の文字列の色に対してなにかしたい
セルの文字色に対してなら
Sub 色でなにかしたい() If Range("A1").Font.ColorIndex = 3 Then '赤色 Range("B1").Value = "なにか" End If End Sub
セル内の文字列の特定の色に対してなにかしたいとき
Sub 色でなにかしたい2() Dim idx As Integer With Range("A1") For idx = 1 To Len(.Value) If .Characters(idx, 1).Font.Color = 255 Then '赤色 Range("B1").Value = "なにか" Exit For End If Next End With End Sub
こんな感じ
追記
こんな関数を作ると
Function colorCheck(cell As Excel.Range, color As Long) As Boolean Dim idx As Integer Dim result As Boolean result = False With cell For idx = 1 To Len(.Value) If .Characters(idx, 1).Font.color = color Then result = True Exit For End If Next End With colorCheck = result End Function
条件付き書式で指定できるかもしれない
2番目の引数は(R+G*256+B*256*256)の値を入れるといいかも
計算が面倒臭い人は
Function colorCheck(cell As Excel.Range, red As Integer, green As Integer, blue As Integer) As Boolean Dim idx As Integer Dim result As Boolean result = False With cell For idx = 1 To Len(.Value) If .Characters(idx, 1).Font.color = RGB(red,green,blue) Then result = True Exit For End If Next End With colorCheck = result End Function
とやって、赤緑青にそれぞれ0~255の値を指定するのもアリかと
はてなブログを登録してみた
とりあえず
できたので
寝る