【问题标题】:How to add Icons from "conditional formatting>Icon sets" to individual cells?如何将“条件格式>图标集”中的图标添加到单个单元格?
【发布时间】:2020-09-07 15:19:38
【问题描述】:

我正在尝试将 Excel 中“条件格式 -> 图标集”中的图标(交通信号灯或菱形等)添加到单个单元格中。

我只在文档中找到了这个:https://docs.microsoft.com/en-us/office/vba/api/excel.iconset 但是我找不到任何不需要条件的示例代码。

假设我想将黄色圆圈添加到单元格“A1”,我该怎么做?

谢谢!

【问题讨论】:

  • 没有条件是不可能的,因为图标来自条件格式。

标签: excel vba formatting


【解决方案1】:

假设我想将黄色圆圈添加到单元格“A1”,我该怎么做?

正如@Pᴇʜ 提到的,这些图标来自图标集,不能在没有条件格式的情况下使用。话虽如此,我们可以伪造红绿灯。

逻辑

  1. 您可以在 Insert | 中使用一个符号。符号 菜单。如下图所示

  2. 只需在单元格文本的开头插入符号并将字体更改为 Wingdings 并将 Forecolor 更改为相关颜色。也可以随意设置字体大小。

代码

Option Explicit

Const TrafficLightSignal As String = "l"

Sub Sample()
    Dim ws As Worksheet
    
    '~~> Change this to the relevant worksheet
    Set ws = Sheet1
    
    InsertTrafficLightSignal ws.Range("A1"), -16727809 '<~~ Orange
    InsertTrafficLightSignal ws.Range("A2"), -11489280 '<~~ Green
    InsertTrafficLightSignal ws.Range("A3"), -16776961 '<~~ Red
End Sub

Private Sub InsertTrafficLightSignal(rng As Range, SignalColor As Long)
    Dim aCell As Range
    
    For Each aCell In rng
        With aCell
            '~~> You can add a space between them as well if you want
            '.Value = TrafficLightSignal & " " & aCell
            .Value = TrafficLightSignal & aCell

            With .Characters(Start:=1, Length:=1).Font
                .Name = "Wingdings"
                .FontStyle = "Regular"
                .Size = 17 '<~~ Change Font size as applicable
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .Color = SignalColor '<~~ Color is changed here
                .TintAndShade = 0
                .ThemeFont = xlThemeFontNone
            End With
        End With
    Next aCell
End Sub

行动中

替代图标

如果你愿意,你也可以选择这个图标

为此,在上面的代码中使用这些代替

Const TrafficLightSignal As String = "n"

.Name = "Webdings" 'instead of Wingdings

输出

无论您选择什么符号,请确保它是黑色的(即填充),以便可以更改字体颜色。

【讨论】:

  • 非常感谢,我想我可以用这个了。我也非常感谢随附的代码,一定会使用它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 2017-06-22
  • 1970-01-01
  • 2020-05-05
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多