代码之家  ›  专栏  ›  技术社区  ›  Maury Markowitz

在Excel*中选择主题颜色而不使用RGB值?

  •  2
  • Maury Markowitz  · 技术社区  · 6 年前

    我收到了一张单子,我正在努力匹配他们挑选的深蓝。

    当我单击主条中“填充图标”旁边的三角形以显示颜色选择器时,我可以看到颜色是第三列底部的第二个颜色,即蓝色。它被称为“深蓝色,文本2,25%深”。

    为了清晰起见,我想用vba做这个,并用英语术语。我尝试了各种列出VBA颜色名称的网页,但没有。有没有什么方法可以在不使用数字RGB或类似方法的情况下选择此选项?

    3 回复  |  直到 6 年前
        1
  •  2
  •   Pspl    6 年前

    我认为您必须使用以下属性:

       .ThemeColor = xlThemeColorAccent5 'name of the theme
       .TintAndShade = 0.25 'darkness (1 - no dark; -1 - max dark)
    
        2
  •  2
  •   Vityata    6 年前

    选中此项可查看样式的颜色:

    Sub TestMe()
    
        Dim st As Style
        Dim cnt As Long: cnt = 1
    
        For Each st In ThisWorkbook.Styles
            Cells(cnt, 1).Interior.ColorIndex = cnt
            Cells(cnt, 2) = cnt
            Cells(cnt, 3) = st.Name
            Cells(cnt, 4) = st.NameLocal
            cnt = cnt + 1
        Next st
    
    End Sub
    

    返回此:

    enter image description here

    如果要对特定选择使用特定样式:

    selection.interior.colorindex = ThisWorkbook.Styles("40 % - Akzent3").Interior.ColorIndex
    
        3
  •  1
  •   user3279536    6 年前
    Sub DemoThemecolors()
       Dim rng As Range
       Dim n As Integer, m As Integer
       Dim arrNames
       Dim arrDescriptions
       Dim arrValues
       arrNames = Array("xlThemeColorAccent1", "xlThemeColorAccent2", "xlThemeColorAccent3", "xlThemeColorAccent4", "xlThemeColorAccent5", "xlThemeColorAccent6", _
                        "xlThemeColorDark1", "xlThemeColorDark2", "xlThemeColorFollowedHyperlink", "xlThemeColorHyperlink", "xlThemeColorLight1", "xlThemeColorLight2")
       arrDescriptions = Array("Accent1", "Accent2", "Accent3", "Accent4", "Accent5", "Accent6", "Dark1", "Dark2", "Followed hyperlink", "Hyperlink", "Light1", "Light2")
       arrValues = Array(5, 6, 7, 8, 9, 10, 1, 3, 12, 11, 2, 4)
    
       ActiveWorkbook.Worksheets.Add
       Set rng = Cells(2, 7)
       rng(0, 4).Value = "TintAndShade"
       rng(1, 4).Value = 0
       For m = 1 To 9
          rng(1, m + 4).Value = 0.1 * m
       Next m
       rng(1, 1) = "ThemeColor Name"
       rng(1, 2).Value = "Value"
       rng(1, 3).Value = "Description"
    
       For n = 1 To 12
          rng(n + 1, 1).Value = arrNames(n)
          rng(n + 1, 2).Value = arrValues(n)
          rng(n + 1, 3).Value = arrDescriptions(n)
          rng(n + 1, 4).Interior.ThemeColor = arrValues(n)
          For m = 1 To 9
             With rng(n + 1, m + 4).Interior
                .ThemeColor = arrValues(n)
                .TintAndShade = 0.1 * m
             End With
          Next m
       Next n
        Range("G1:S2").Font.Bold = True
        Columns("G:I").EntireColumn.AutoFit
    End Sub
    

    如果你想知道为什么所有内置的主题都会在light1/2和light1/2中显示深色-这可能是一个bug,或者微软称之为它;它在Excel和Word中也是一个“特性”。在PowerPoint中,反之亦然——更多的是你所期望的那样。

    推荐文章