Vba 颜色形状根据其文字
我有一张有几个形状的表格,上面有文本字符串,我想根据它的文本给这些形状上色。这是我的代码,目前它不能像预期的那样工作Vba 颜色形状根据其文字,vba,excel,Vba,Excel,我有一张有几个形状的表格,上面有文本字符串,我想根据它的文本给这些形状上色。这是我的代码,目前它不能像预期的那样工作 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim shp As Shape, r As Long, g As Long, b As Long, NormScale As String With ActiveSheet For Each shp In .Shapes With
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim shp As Shape, r As Long, g As Long, b As Long, NormScale As String
With ActiveSheet
For Each shp In .Shapes
With shp.TextFrame
Select Case NormScale
Case "N"
r = 255
g = 0
b = 0
Case "P"
r = 128
g = 128
b = 128
End Select
End With
shp.Fill.ForeColor.RGB = RGB(r, g, b)
Next shp
End With
End Sub
你只是忘了读课文:
Sub Mike()
Dim shp As Shape, r As Long, g As Long, b As Long, NormScale As String
With ActiveSheet
For Each shp In .Shapes
With shp.TextFrame
NormScale = .Characters.Text
Select Case NormScale
Case "N"
r = 255
g = 0
b = 0
Case "P"
r = 128
g = 128
b = 128
End Select
End With
shp.Fill.ForeColor.RGB = RGB(r, g, b)
Next shp
End With
End Sub
编辑#1:
要从流程中排除特定形状,我们必须首先确定,然后:
Sub WhatDoWeHave()
Dim shp As Shape
With ActiveSheet
For Each shp In .Shapes
MsgBox shp.Type & vbCrLf & shp.Name
Next shp
End With
End Sub
编辑#2:
此版本将排除名称以“Picture”开头的形状
什么意思
不能按预期工作?程序崩溃了吗?只涂一种颜色?不应用任何颜色?它将工作表中的所有形状都涂成黑色。显示错误“对象不支持属性或方法”。@Mike这意味着工作表中的某些形状不支持Text.Characters。我用自选图形对代码进行了测试,表中也有图片。我可以排除它们吗?或者是否有一些解决方法?必须排除图1、图2、图3和图4。这些是名字。
Sub Mike()
Dim shp As Shape, r As Long, g As Long, b As Long, NormScale As String
With ActiveSheet
For Each shp In .Shapes
If InStr(shp.Name, "Picture") = 0 Then
With shp.TextFrame
NormScale = .Characters.Text
Select Case NormScale
Case "N"
r = 255
g = 0
b = 0
Case "P"
r = 128
g = 128
b = 128
End Select
End With
shp.Fill.ForeColor.RGB = RGB(r, g, b)
End If
Next shp
End With
End Sub