【问题标题】:Sum of Colored Special Characters彩色特殊字符的总和
【发布时间】:2022-07-18 06:15:59
【问题描述】:

我正在尝试计算每列的模块 + 小时数。

使用的符号是特殊字符+彩色。

在 Excel 单元格中显示“u u”

  • 红色和黄色的“u u”表示 2 小时
  • 绿色和黄色的“u u”表示 1h
  • 蓝色和黄色的“u u”表示 0.5h

可以对字符求和吗?

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

列表(无法显示名称)这些列表大约有 50 个

【问题讨论】:

  • 你能展示一下这些文件的样子吗?
  • 在您的位置上,我会尝试影响制作您提供的表格的人,以将他们表示事物的方式更改为更易于管理的形式。
  • 谢谢 但是我已经尝试了 1 年。这不会很快改变。对不起

标签: excel vba excel-formula colors special-characters


【解决方案1】:

第一件事很简单:单元格文本实际上是u u,只是用字体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

【讨论】:

    【解决方案2】:

    这是我的两分钱:

    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”的颜色代码。

    【讨论】:

    • 先生,超级棒!绝对需要收藏。
    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 2015-07-10
    • 2012-09-24
    • 2011-05-17
    • 2016-12-08
    • 2011-05-03
    • 2019-09-05
    相关资源
    最近更新 更多