【點選儲存格,則該列底色反黃變色-多選版】
繼先前針對單選儲存格,底色動態變色的範例應用後
(〔VBA〕範例:動態螢光棒(列變色)的作法[格式化的條件]--v010)
我們再進一步針對多選列,來進行底色變色的"實驗吧"
作法如同單選變色的方式,只是增加了選取區域(Areas)的判斷
針對每個選取列,來增設「設定格式化的條件」
語法:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Range("C4:W33").FormatConditions.Delete '將C4:W33內格式化條件的設定刪除
Dim tRange As Range, tRow As Range
For Each tRange In Target.Areas
For Each tRow In tRange.Rows
'只針對範圍設定在C4~W33
If tRow.Row > 3 And tRow.Row < 34 Then
'設定該區的格式化條件
With tRow.EntireRow.Range("C1:W1").FormatConditions
.Add xlExpression, , "TRUE"
.Item(1).Interior.ColorIndex = Int(36)
.Item(1).StopIfTrue = False
End With
End If
Next
Next
Set tRange = Nothing: Set tRow = Nothing
End Sub
補充:
With tRow.EntireRow.Range("C1:C1").FormatConditions
.Add xlExpression, , "TRUE"
With .Item(1).Font '字型
.Color = -11489280
.Bold = True
.Italic = False
End With
With .Item(1).Borders(xlBottom) '下框線
.LineStyle = xlContinuous
.Color = -16776961
.Weight = xlThin
End With
With .Item(1).Interior '填滿
.PatternColorIndex = xlAutomatic
.Color = 65535
End With
.Item(1).StopIfTrue = False
End With
相關課程:
〔VBA〕範例:動態螢光棒(列變色)的作法[格式化的條件]--v010
章老師的電腦小講堂 https://www.facebook.com/ScenicSchool/
※※ 本區做為經常使用之程式碼複製區,提問請至小講堂唷 ※※