gpt4 book ai didi

vba - Excel VBA 中出现错误 70 的原因是什么?

转载 作者:行者123 更新时间:2023-12-04 21:08:57 29 4
gpt4 key购买 nike

我有一些代码不断导致

Error 70: Permission Denied

在我的 VBA 代码中。我不知道为什么,因为我知道工作表不 protected ,我可以对其进行更改。有问题的代码是
sh.Name = "square"

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

关于可能导致此权限错误的任何建议?

最佳答案

通常,这是由于尝试使用相同的名称两次造成的。尝试这样做:

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

关于vba - Excel VBA 中出现错误 70 的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/948950/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com