什么导致Excel VBA中的错误70?

我有一些代码,不断造成一个

Error 70: Permission Denied 

在我的VBA代码。 我不知道为什么,因为我知道工作表是不受保护的,我可以对其进行更改。 有问题的代码是

 sh.Name = "square" 

它会尝试重命名从另一个工作表复制并粘贴到工作表中的形状 – 工作表中没有具有该名称的其他形状,因为在这些代码之前,我已经删除了具有该名称的所有形状。

任何build议什么可能会导致此权限错误?

一般来说,一个是由于尝试使用相同的名称两次造成的。 尝试这样做,而不是:

 Sub Example() Dim lngIndx As Long Dim ws As Excel.Worksheet Dim shp As Excel.Shape Set ws = Excel.ActiveSheet Set shp = ws.Shapes.AddShape(msoShapeOval, 174#, 94.5, 207#, 191.25) If NameUsed(ws, "Foo") Then lngIndx = 2 Do While NameUsed(ws, "Foo" & CStr(lngIndx)) lngIndx = lngIndx + 1 Loop shp.name = "Foo" & CStr(lngIndx) Else shp.name = "Foo" End If End Sub Private Function NameUsed(ByVal parent As Excel.Worksheet, ByVal name As String) As Boolean Dim shp As Excel.Shape Dim blnRtnVal As Boolean name = LCase$(name) For Each shp In parent.Shapes If LCase$(shp.name) = name Then blnRtnVal = True Exit For End If Next NameUsed = blnRtnVal End Function 

清洁,当你走。 将对象设置为空,string在使用后为空,且不在函数和子例程之间使用相同的名称。

“权限被拒绝”不是用于受保护的工作表,而是用于访问属性或variables的错误。

我相信在您尝试访问它并设置其“名称”属性时,“sh”为空。 尝试查看是否在设置其属性之前正确初始化了它。