vba 改变形状的背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11552930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:35:15  来源:igfitidea点击:

Change backcolor of shape

excel-vbabackcolorvbaexcel

提问by AndroidDev

I'm trying to update the backcolor of all of the labels that I have on a worksheet. I'd like to indicate the color using the RGB values, but I'm stuck in two places. Here is the code that I have right now:

我正在尝试更新工作表上所有标签的背景色。我想使用 RGB 值来表示颜色,但我被困在两个地方。这是我现在拥有的代码:

Sheet2.Shapes("Label 2").Fill.BackColor.RGB = RGB(220, 105, 0)

This code will run without error, but it seems to have no effect. My label starts out white (or maybe transparent) and never changes. Can anyone tell me what I need to do to make this work? I also added this but it did nothing:

这段代码运行时不会出错,但似乎没有任何效果。我的标签一开始是白色的(或者可能是透明的)并且永远不会改变。谁能告诉我我需要做什么才能使这项工作?我也添加了这个,但它什么也没做:

shp.Fill.Solid

Next, I'd like to capture this RGB value in a variable so that I don't have to re-type of repeatedly. Essentially, I'm looking for something like this:

接下来,我想在一个变量中捕获这个 RGB 值,这样我就不必重复地重新输入。本质上,我正在寻找这样的东西:

dim col as Color
col = RGB(220,105,0)
Sheet2.Shapes("Label 2").Fill.BackColor.RGB = col

I know that there is no variable type called Color, but I think you can see what I am trying to do.

我知道没有名为 Color 的变量类型,但我认为您可以看到我想要做什么。

回答by Jon Crowell

Try setting the ForeColor instead:

尝试改为设置 ForeColor:

Sheet2.Shapes("Label 2").Fill.ForeColor.RGB = RGB(220, 105, 0)

A good way to figure out what to do is to record a macro while you make the adjustment yourself. You can examine the generated code afterward and use it as a starting point.

弄清楚该怎么做的一个好方法是在您自己进行调整的同时录制一个宏。您可以随后检查生成的代码并将其用作起点。



Here is an example of a procedure that will add a rectangle to the activesheet, add some text to it, and then color it with your RGB values:

下面是一个过程示例,该过程将向活动表添加一个矩形,向其中添加一些文本,然后使用您的 RGB 值对其进行着色:

Public Sub AddRectangleWithText()
    Dim textRectangle As Shape

    Set textRectangle = ActiveSheet.Shapes & _
        .AddShape(msoShapeRectangle, 10, 80, 250, 50)

    ' add your text
    textRectangle.TextFrame.Characters.Text = "Your mother was a hamster."

    ' fill the shape with the rgb color of your choice
    textRectangle.Fill.ForeColor.RGB = RGB(220, 105, 0)

End Sub

回答by GilBon

ForeColoractually controls the backcolor of comment/textbox(s) in Excel as follows;

ForeColor实际上控制 Excel 中注释/文本框的背景色如下;

  • Activecell.Comment.Shape.Fill.ForeColor.RGB = RGB(240, 255, 250) 'Mint Green
  • Activecell.Comment.Shape.Fill.ForeColor.RGB = RGB(240, 255, 250) 'Mint Green

as the Comment/TextBoxForeColoris a foreground fill over the Applicatoin background color.

因为Comment/TextBoxForeColor是 Applicatoin 背景颜色上的前景填充。

回答by j-hap

I needed to add

我需要添加

textRectangle.Fill.Visible = msoTrue

in Office 2016 for

在 Office 2016 中

textRectangle.Fill.ForeColor.RGB = RGB(220, 105, 0)

to have an effect.

有效果。