Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xA As Range, xR As Range
Cells.Interior.Pattern = 0
If Target.Column = 3 Then
Set xA = [A4:G1000]
If Intersect(xA, Target) Is Nothing Then Exit Sub
For Each xR In xA.Rows
If Not Intersect(xR, Target) Is Nothing Then xR.Interior.Color = RGB(240, 255, 240)
Next
End If
End Sub作者: av8d 時間: 2021-9-4 12:48
請測試看看,謝謝
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xA As Range, xR As Range
Set xA = [A1:XFD65536] '設定範圍
If Intersect(xA, Target) Is Nothing Then Exit Sub
xA.Interior.ColorIndex = 0
Intersect(Target, xA).Interior.Color = RGB(255, 255, 0)
End Sub作者: av8d 時間: 2021-9-5 20:36
謝謝前輩假日抽空協助,我意思是既有的情況(儲存格有則範圍列變色,無則單一儲存格變色)
我修改了前輩的程式碼後為
If Intersect(xA, Target) Is Nothing Then
xA.Interior.ColorIndex = 0
Intersect(Target, xA).Interior.Color = RGB(240, 255, 240)
Else
For Each xR In xA.Rows
If Not Intersect(xR, Target) Is Nothing Then xR.Interior.Color = RGB(240, 255, 240)
Next
End If
但是功能還是只有範圍變色,沒有單一儲存格變色,受益良多,謝謝前輩。作者: quickfixer 時間: 2021-9-5 20:56
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xA As Range, xR As Range
Cells.Interior.Pattern = 0
If Target.Column = 3 Then
Set xA = [A4:G1000]
If Intersect(xA, Target) Is Nothing Then Exit Sub
For Each xR In xA.Rows
If Not Intersect(xR, Target) Is Nothing Then
If Cells(xR.Row, 3) <> "" Then
xR.Interior.Color = RGB(240, 255, 240)
Else
Cells(xR.Row, 3).Interior.Color = RGB(200, 200, 240)
End If
End If
Next
End If
End Sub作者: samwang 時間: 2021-9-5 21:04
要刪除後立刻變色,在#7多加入下面3行
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 3 Then Call Worksheet_SelectionChange(Target)
End Sub作者: av8d 時間: 2021-9-6 01:48
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xA As Range, xR As Range
Call 顏色
Set xA = [A4:G1000]
If Target.Column = 3 Then
'Set xA = [A4:G1000]
xA.Interior.Pattern = 0
If Intersect(xA, Target) Is Nothing Then Exit Sub
For Each xR In xA.Rows
If Not Intersect(xR, Target) Is Nothing Then
If Cells(xR.Row, 3) <> "" Then
xR.Interior.Color = GBY
Else
Cells(xR.Row, 3).Interior.Color = GBY
End If
End If
Next
Else
xA.Interior.Pattern = 0
End If
End Sub作者: quickfixer 時間: 2021-9-9 15:36
If Intersect(xA, Target) Is Nothing Then Exit Sub
這段的意思是?
>> 可以利用F1查詢,Intersect: 會傳回代表兩個或多個範圍的矩形交集的 Range 物件。 如果指定的一個或多個範圍來自不同的工作表,則會傳回錯誤。
所以如過選擇是Target:3 col但沒有在xA範圍就離開 作者: av8d 時間: 2021-10-2 01:31