Excelで上の行とデータ内容が被った場合に次の行のデータの文字色を薄くする
- こんなデータを
- こうする
VBA
' 開始行
Public Const FirstRow = "5"
' 変更後の文字色
Public Const DupCellColor = "&HAAAAAA"
' メイン処理
Sub UpdateDilColor()
Dim LoopCnt As Integer ' ループカウンタ
Dim CellColArray As Variant ' 対象列の配列
CellColArray = Array("B", "C", "D", "E", "F") ' 配列初期化
Dim CurRange As Range ' 現在行
Dim NextRange As Range ' 次の行
' 対象列の配列分ループ
For LoopCnt = LBound(CellColArray) To UBound(CellColArray)
' 現在行セルをセット
Set CurRange = Range(CellColArray(LoopCnt) & FirstRow)
' 次の行セルをセット
Set NextRange = GetNextRowRange(CurRange)
' 色更新
Call ChangeColor(CurRange, NextRange)
Next LoopCnt
End Sub
' 次の行セル取得
Function GetNextRowRange(argRange As Range) As Range
Dim val As Range
Set GetNextRowRange = argRange.Offset(RowOffset:=1)
End Function
' 色変換(選択列の最下行まで再帰呼び出しでループ)
Sub ChangeColor(argCurRange As Range, argNextRange As Range)
Dim CurRange As Range
Dim NextRange As Range
Set CurRange = argCurRange
Set NextRange = argNextRange
' 次の行セルが空なら再帰呼び出し終了(処理なし)
If NextRange.Value <> "" Then
' 現在セルと次の行セルがダブっていたら次の行セルの文字色変更
If CurRange.Value = NextRange.Value And NextRange.Value <> "-" Then
If CurRange.Offset(, columnOffset:=-1).Value = NextRange.Offset(, columnOffset:=-1).Value Then
NextRange.Font.Color = CLng(DupCellColor)
End If
End If
' 現在セル、次の行セルを1行移動
Set CurRange = GetNextRowRange(CurRange)
Set NextRange = GetNextRowRange(NextRange)
' 再帰呼び出し
Call ChangeColor(CurRange, NextRange)
End If
End Sub
- 上と同じデータだけど、左で切り替わりがあったから別データとみなす、みたいなことをしてる部分が汚いけどまぁよしとしよう