你好 Stackoverflow 社區
我試圖自動計算每列的模塊 小時數。最終我放棄了,因為我沒有足夠的經驗,而且使用的符號是特殊字符 彩色。
在 Excel 單元格中,它顯示“u u”
- 紅色和黃色的“u u”表示 2h
- 綠色和黃色的“u u”表示 1h
- 藍色和黃色的“u u”表示 0.5h
是否可以制作一個 VBA 公式來總結字符?
到目前為止我嘗試使用的代碼是:
Public Function SumColorRed(pRange1 As Range, pRange2 As Range) As Double
Application.Volatile
Dim rng As Range
For Each rng In pRange1
If rng.Font.Color = pRange2.Font.Color Then
SumColor = SumColor 2
End If
Next
End Function
先感謝您

uj5u.com熱心網友回復:
第一件事很簡單:單元格文本是真的u u,只是用 Font WinDings 格式化,它會顯示一個◆
如果rng.Font.Color用于讀取或設定顏色,則表示該顏色對整個單元格有效。如果要獲取(或設定)單個字符的顏色(或其他屬性,如粗體或斜體),可以使用Characters-Property。您需要指定開始和長度作為引數,例如rng.Characters(3, 1)到單元格的第三個字符。
以下代碼查看單元格的第一個和第三個字符并檢查顏色。我不能 100% 確定我的顏色定義與您的作業表中使用的顏色完全相同,也許您必須調整常量定義。
Function getColorTime(cell As Range) As Date
Const redCharColor = &HFF&
Const yellowCharColor = &HC0FF&
Const greenCharColor = &H50B000
Const blueCharColor = &HC07000
Dim c1 As Long, c2 As Long
c1 = cell.Characters(1, 1).Font.Color
c2 = cell.Characters(3, 1).Font.Color
' Debug.Print c1, c2
If c1 = redCharColor And c2 = yellowCharColor Then
getTime = TimeSerial(2, 0, 0)
ElseIf c1 = yellowCharColor And c2 = greenCharColor Then
getTime = TimeSerial(1, 0, 0)
ElseIf c1 = blueCharColor And c2 = greenCharColor Then
getTime = TimeSerial(0, 30, 0)
End If
End Function
uj5u.com熱心網友回復:
這是我的兩分錢:

中的公式C1:
=CountColor(A1:A4)
參考UDF:
Function CountColor(rng As Range) As Double
Dim cl As Range
For Each cl In rng
Select Case cl.Characters(1, 1).Font.Color & "|" & cl.Characters(3, 1).Font.Color
Case "255|49407"
CountColor = CountColor 2
Case "12874308|4697456"
CountColor = CountColor 1
Case "49407|4697456"
CountColor = CountColor 0.5
Case Else
CountColor = CountColor
End Select
Next
End Function
顯然,您想找出“ace”的顏色代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/465691.html
