And*_*Dev 2 excel vba backcolor excel-vba
我正在尝试更新我在工作表上的所有标签的背景颜色.我想用RGB值来表示颜色,但是我被困在两个地方.这是我现在的代码:
Sheet2.Shapes("Label 2").Fill.BackColor.RGB = RGB(220, 105, 0)
Run Code Online (Sandbox Code Playgroud)
此代码将运行没有错误,但似乎没有任何效果.我的标签开始是白色(或可能透明),永远不会改变.任何人都可以告诉我我需要做些什么来使这项工作?我也添加了这个,但它没有做任何事:
shp.Fill.Solid
Run Code Online (Sandbox Code Playgroud)
接下来,我想在变量中捕获此RGB值,以便我不必重复重新键入.基本上,我正在寻找这样的东西:
dim col as Color
col = RGB(220,105,0)
Sheet2.Shapes("Label 2").Fill.BackColor.RGB = col
Run Code Online (Sandbox Code Playgroud)
我知道没有名为Color的变量类型,但我认为你可以看到我想要做的事情.
Jon*_*ell 10
尝试设置ForeColor:
Sheet2.Shapes("Label 2").Fill.ForeColor.RGB = RGB(220, 105, 0)
Run Code Online (Sandbox Code Playgroud)
弄清楚要做什么的好方法是在自己进行调整时记录宏.您可以在以后检查生成的代码并将其用作起点.
下面是一个过程示例,它将向活动表添加一个矩形,向其中添加一些文本,然后使用您的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
Run Code Online (Sandbox Code Playgroud)