What causes Error 70 in Excel VBA?

后端 未结 4 617
逝去的感伤
逝去的感伤 2021-01-24 04:26

I have some code which keeps causing an

Error 70: Permission Denied

in my VBA code. I can\'t work out why, because I know that the worksheet i

相关标签:
4条回答
  • 2021-01-24 04:42

    "Permission Denied" is not for a protected worksheet but for wrong access to a property or variable.

    I believe that "sh" is null at the point you are trying to access it and set its "Name" property. try to see if you initialized it correctly before setting its properties.

    0 讨论(0)
  • 2021-01-24 04:49

    Generally that one is caused by trying to use the same name twice. Try doing this instead:

    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
    
    0 讨论(0)
  • 2021-01-24 04:50

    There are several answers here on StackOverflow about this VB Error. Each answer or situation is unique in reality - although each existing answer states a different potential root cause (file permissions, folder permissions, name reuse, ranges, etc).

    I would recommend narrowing down the root-cause by double clicking on the side of the stating function/code in order to mark a breakpoinnt (looks like a red dot) (Alternatively, you can right click on the line of the code - Select the Toggle and then Breakpoint).

    Next, run your code, and it will stop in your breakpoint. You can then Step-Into/Over/Out your code and essentially find the line of code that is responsible for throwing your error code. (Step Into is F8, Step over is Shift+F8 ((Go To the Debug top menu to see more options)))

    Once you identified the responsible line of code - you can start looking further.

    In my case scenario, I was using a protected variable name "Date" (look into variable names). Once I renamed it to something else, the problem was fixed.

    0 讨论(0)
  • 2021-01-24 05:05

    Clean as you go. Set objects to nothing, strings to nullstring after using them and don't use the same names between functions and subroutines.

    0 讨论(0)
提交回复
热议问题