得到一个“编译错误:无法分配给只读属性”为以下几点:无法分配给只读属性VBA
With wsData.Shapes("Rectangle 1").Fill
.Pattern = xlGrid
.ForeColor.RGB = RGB(255, 0, 0)
End With
得到一个“编译错误:无法分配给只读属性”为以下几点:无法分配给只读属性VBA
With wsData.Shapes("Rectangle 1").Fill
.Pattern = xlGrid
.ForeColor.RGB = RGB(255, 0, 0)
End With
你缺少中间的ShapeRange
属性:
With wsData.Shapes("Rectangle 1").ShapeRange
全码:
With wsData.Shapes("Rectangle 1").ShapeRange
With .Fill
.Pattern = xlGrid
.BackColor.RGB = RGB(255, 0, 0)
End With
End With
编辑1:代码可能出现的错误处理
Option Explicit
Sub ColorChape()
Dim wsData As Worksheet
Dim myShp As Shape
Set wsData = Worksheets("Sheet1") ' <-- modify to your sheet's name
On Error Resume Next
Set myShp = wsData.Shapes("Rectangle 1")
On Error GoTo 0
If myShp Is Nothing Then ' <-- unable to set the shape, doesn't exist in specified sheet
MsgBox "`Rectangle 1` Shape doesn't exist in " & wsData.Name & " sheet!", vbCritical
Else
With myShp
With .Fill
.BackColor.RGB = RGB(255, 0, 0)
' rest of your code goes here
End With
End With
End If
End Sub
你有问题吗? – CLR
尝试'.BackColor.RGB = RGB(255,0,0)' –
谢谢大卫。但是,我仍然收到此错误 - “编译错误:无法分配给只读属性”。 – ajdesanti