首页 > 解决方案 > 是否可以使用自定义颜色来使用 VBA 填充单元格?

问题描述

我正在使用颜色索引 20 来填充一些单元格,但这不是我对这个项目的首选颜色。我会使用不在颜色索引调色板中的自定义浅灰色。是否可以?

这是我的代码:

Sheets("Panel").Range("E7:E31").Interior.ColorIndex = 20

标签: excelvba

解决方案


单元格颜色选择器

  • 您可以将所需的颜色应用于单元格并运行以下程序,选择单元格并按ENTER或选择OK,颜色值将复制到剪贴板,等待您使用CTRL+ V

编码

Option Explicit

Sub CellColorPicker()
    Dim rgAddress As String
    If TypeName(Selection) = "Range" Then
        rgAddress = Selection.Cells(1).Address
    Else
        rgAddress = "A1"
    End If
    Dim rg As Range
    On Error Resume Next
    Set rg = Application.InputBox("Pick a cell", "ColorPicker", _
        rgAddress, , , , , 8)
    On Error GoTo 0
    If Not rg Is Nothing Then
        With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
            .SetText rg.Cells(1).Interior.Color
            .PutInClipboard
        End With
    End If
End Sub

推荐阅读