#1です。
$ は固定を意味し、固定したい部分の前につけます。
$E1は、列は固定で行は相対、E$1は、列は相対で行は固定、$E$1は、列も行も固定
C1:I1 を選択した状態で =AND($E1="○",$F1="○",$G1="○",$H1="○",$I1="○") と入れた場合は全てのセルがE1~I1の状況を判断して書式を設定します。
それに対して、列を相対にして同じ事をすると
C1 は =AND(E1="○",F1="○",G1="○",H1="○",I1="○")
D1 は =AND(F1="○",G1="○",H1="○",I1="○",J1="○")
E1 は =AND(G1="○",H1="○",I1="○",J1="○",K1="○")
・
・
のように判定対象が相対的にずれます。
AND関数は 全てが True の場合に True を返すので、C1は色が付いてもD1以降は色が付きません。
---
条件付き書式で解決したようですが、VBAでも近い動作を書いて見ました。
これは標準モジュールではなく、シートモジュールに書きます。
シート名のタブを右クリックして「コードの表示」を押下し、出て来たVBE画面にコピペします。
該当シートのE:Hに○を入力すると黄色、Iにも○を入力すると水色になります。
試す場合は新規ブックでどうぞ。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, i As Integer, flg As Byte
For Each r In Target
If Not Application.Intersect(r, Me.Columns("E:I")) Is Nothing Then
With r.EntireRow
flg = 1
For i = 5 To 8
If .Cells(1, i).Value <> "○" Then
flg = 0
Exit For
End If
Next i
If .Cells(1, 9).Value = "○" Then flg = flg + 2
Select Case flg
Case 1
.Cells(1, 3).Resize(1, 6).Interior.ColorIndex = 6
.Cells(1, 9).Interior.ColorIndex = xlNone
Case 3
.Cells(1, 3).Resize(1, 7).Interior.ColorIndex = 8
Case Else
.Cells(1, 3).Resize(1, 7).Interior.ColorIndex = xlNone
End Select
End With
End If
Next r
End Sub
お礼
お陰様で思い通りの条件付き書式が作成できました。 ありがとうございます。 ただ新たな疑問が生まれました。。。 まず、C~I列まで全て選択した状態で、 E1~I1まで全て○の場合、C1~I1までのセルの色を変える =AND($E1="○",$F1="○",$G1="○",$H1="○",$I1="○") の場合うまくいきましたが、 =AND(E1="○",F1="○",G1="○",H1="○",I1="○") の場合C1のセルのみ色が変わり、他のセルは元の色のままでした。 なんとなく、$をつけただけで、この意味がわかりませんでした。 相対参照、絶対参照の意味はわかるんですが。。。 なぜなんでしょうか?
補足
早速の回答ありがとうございます。 単一の色でなく、○の数によって、セルの色を変えたいのですが、 条件付き書式で可能なのでしょうか? 初めてで、こんな無駄に長いプログラムになってしまいました。。。 Sub セル色() With ActiveCell If .Offset(, -5).Value = "○" And .Offset(, -4).Value = "○" And .Offset(, -3).Value = "○" And .Offset(, -2).Value = "○" And .Offset(, -1).Value <> "○" Then Selection.Offset(0, -7).Select With Selection.Interior .ColorIndex = 6 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 6 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 6 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 6 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 6 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 6 .Pattern = xlSolid End With ElseIf .Offset(, -5).Value = "○" And .Offset(, -4).Value = "○" And .Offset(, -3).Value = "○" And .Offset(, -2).Value = "○" And .Offset(, -1).Value = "○" Then Selection.Offset(0, -7).Select With Selection.Interior .ColorIndex = 8 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 8 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 8 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 8 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 8 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 8 .Pattern = xlSolid End With Selection.Offset(0, 1).Select With Selection.Interior .ColorIndex = 8 .Pattern = xlSolid End With End If End With End Sub