請測試看看,謝謝
Sub test()
Arr = Range("a1:e7") '資料範圍
Cells.Interior.ColorIndex = 0 '無顏色
For j = 1 To UBound(Arr, 2): For i = 2 To UBound(Arr)
If UCase(Arr(i, j)) = "A" Then Cells(i, j).Interior.ColorIndex = 3
If UCase(Arr(i, j)) = "B" Then Cells(i, j).Interior.ColorIndex = 6
If UCase(Arr(i, j)) = "C" Then Cells(i, j).Interior.ColorIndex = 5
Next i: Next j
End Sub作者: hcm19522 時間: 2021-10-29 10:35
工作表模組:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
With Target
If .Row < 3 Or .Value = "" Then Exit Sub
If .Column Mod 4 <> 1 Then Exit Sub
Cancel = True
Call 變字色_多個同股名
If Y(.Value & "|") > 1 Then Y(.Value).Interior.ColorIndex = 4
Set Y = Nothing
Set Brr = Nothing
End With
End Sub
Module1:
Option Explicit
Public Brr, Y
Sub 變字色_多個同股名()
Dim C&, R&
Set Y = CreateObject("Scripting.Dictionary")
ActiveSheet.UsedRange.Offset(2).Font.ColorIndex = 1
ActiveSheet.UsedRange.Offset(2).Interior.ColorIndex = xlNone
Brr = ActiveSheet.UsedRange
Set Y(1) = [B1]
For C = 1 To UBound(Brr, 2) Step 4
For R = 3 To UBound(Brr)
Y(Brr(R, C) & "|") = Y(Brr(R, C) & "|") + 1
If Y(Brr(R, C) & "|") = 1 Then
Set Y(Brr(R, C)) = Cells(R, C)
GoTo PASS
End If
If Y(Brr(R, C) & "|") = 2 Then
Set Y(1) = Union(Y(1), Y(Brr(R, C)))
End If
Set Y(1) = Union(Y(1), Cells(R, C))
Set Y(Brr(R, C)) = Union(Y(Brr(R, C)), Cells(R, C))
PASS:
Next
Next
Y(1).Font.ColorIndex = 5
End Sub作者: coafort 時間: 2023-3-30 10:02
工作表模組:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
With Target
If .Row < 3 Or .Value = "" Or .Count > 1 Then Exit Sub
If .Column Mod 5 <> 1 Then Exit Sub
Cancel = True
Call 變字色_多個同股名
If Y(.Value & "|") > 1 Then Y(.Value).Interior.ColorIndex = 4
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Row < 3 Or .Count > 1 Then Exit Sub
If .Column Mod 5 <> 0 Then Exit Sub
Call 變字色_多個同股名
If Y(.Offset(0, -4) & "|") > 1 Then
Y(.Offset(0, -4) & "").Interior.ColorIndex = 4
Application.EnableEvents = False
Y(.Offset(0, -4) & "/").Value = .Value
Application.EnableEvents = True
Application.Goto Y(.Offset(0, -4) & "/")
End If
End With
End Sub
Module1:
Option Explicit
Public Y
Sub 變字色_多個同股名()
Dim Brr, Crr, C&, i&, X&, xR, R&, T, V, Z, Ad
Set Y = CreateObject("Scripting.Dictionary")
ActiveSheet.UsedRange.Offset(2).Font.ColorIndex = 1
ActiveSheet.UsedRange.Offset(2).Interior.ColorIndex = xlNone
Brr = ActiveSheet.UsedRange
Set Y(1) = [B1]
For C = 1 To UBound(Brr, 2) Step 5
For R = 3 To UBound(Brr)
Y(Brr(R, C) & "|") = Y(Brr(R, C) & "|") + 1
If Y(Brr(R, C) & "|") = 1 Then
Set Y(Brr(R, C)) = Cells(R, C)
Set Y(Brr(R, C) & "/") = Cells(R, C + 4)
GoTo PASS
End If
If Y(Brr(R, C) & "|") = 2 Then
Set Y(1) = Union(Y(1), Y(Brr(R, C)))
End If
Set Y(1) = Union(Y(1), Cells(R, C))
Set Y(Brr(R, C)) = Union(Y(Brr(R, C)), Cells(R, C))
Set Y(Brr(R, C) & "/") = Union(Y(Brr(R, C) & "/"), Cells(R, C + 4))
PASS:
Next
Next
Y(1).Font.ColorIndex = 5
End Sub作者: coafort 時間: 2023-3-30 16:20