2017-04-06 148 views
-2

得到一个“编译错误:无法分配给只读属性”为以下几点:无法分配给只读属性VBA

With wsData.Shapes("Rectangle 1").Fill 

    .Pattern = xlGrid 
    .ForeColor.RGB = RGB(255, 0, 0) 

End With 
+2

你有问题吗? – CLR

+1

尝试'.BackColor.RGB = RGB(255,0,0)' –

+0

谢谢大卫。但是,我仍然收到此错误 - “编译错误:无法分配给只读属性”。 – ajdesanti

回答

1

你缺少中间的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 
+0

嗨,谢谢你的回复。没有,也没有工作。 – ajdesanti

+0

@ajdesanti你确定你有一个'''Rectangle 1''Shape?在'wsData'工作表中? –

+0

错误发生是因为“.Pattern = xlGrid”,因为代码在我运行这个函数时工作正常 - 使用wsData.Shapes(“Rectangle 1”)。填充 .ForeColor.RGB = RGB(255,0,0) End With – ajdesanti

相关问题