Microsoft Excel VBA - Run-Time Error '-2147024

2019-08-18 14:05发布

I recorded a macro that will change the text on the button to show the current date. The worksheet is not protected. When I shared the Workbook, and click on the button, I get this error:

Run-time error '-2147024809 (80070057)':
Requested Shapes are locked for selection

I uncheck the "Lock" and "Lock Text" (when I right click on the button and click "Format Control" and under "Protection" tab), but it didn't resolve the issue.

Does anyone know a workaround for this? I want the macro to work while it is being shared. Thank you.

Here is the code I got when I recorded the macro:

Sub updateDate()
'
' updateDate Macro
'

'
    ActiveSheet.Shapes.Range(Array("Button 1")).Select
    Selection.Characters.Text = Date
    With Selection.Characters(Start:=1, Length:=9).Font
        .Name = "Calibri"
        .FontStyle = "Bold"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = 3
    End With
    Range("B1").Select
End Sub

2条回答
混吃等死
2楼-- · 2019-08-18 14:35

You cannot access some Objects/Class when you had it Shared/Protected. But what your goal is can be achieved, you don't have to change the Fonts once it is done (bare in mind that this Sub should only be called when the ActiveSheet had this button):

Sub updateDate()
'
' updateDate Macro
'
'
    ActiveSheet.Buttons("Button 1").Text = CStr(Now) ' Date
    Range("B1").Select ' <-- Not needed
End Sub


EDIT
To change Shapes when it is Shared, you first need to UnShare it, make changes and then Save it SharedAccess.

Try add another button and assign it to MakeButtonRedBold:

Sub MakeButtonRedBold()
    MakeButtonStyle01 "Button 1"
End Sub

Private Sub MakeButtonStyle01(ByVal sButtonName As String)
    Dim oBtn As Object
    On Error Resume Next
    Set oBtn = ActiveSheet.Buttons("Button 1")
    On Error GoTo 0
    If Not oBtn Is Nothing Then
        Application.DisplayAlerts = False
        ThisWorkbook.ExclusiveAccess ' UnShare Workbook
        With oBtn.Font
            .Name = "Calibri"
            .Bold = True
            .Size = 11
            .Color = RGB(255, 0, 0)
        End With
        ThisWorkbook.SaveAs ThisWorkbook.FullName, AccessMode:=xlShared ' Share the Workbook
        Application.DisplayAlerts = True
    End If
End Sub
查看更多
一纸荒年 Trace。
3楼-- · 2019-08-18 14:46


I got same error when trying to view png files in vb6.0 sp6 win10-64bit. Maybe you could use it.
Short answer: On Error Resume Next

All code that showed a png file, i found out yesterday.
Changed to Image1.image on Form1 because properties: Stretch = True

Private Sub Form_Load()
' To make this work
' In Menu Project/References [x] Microsoft Windows Image Acquisition Library V2.0
' C:\WINDOWS\System32\wiaaut.dll
' http://www.vbforums.com/showthread.php?813811-How-to-load-PNG-image
' Make png-support.exe DID WORK if added line below
On Error Resume Next ' Get rid of error: vb6 runtime error-2147024809

Dim img As New WIA.ImageFile
img.LoadFile "c:\temp\test.png" ' ok filetypes png jpg gif tif
Set Image1 = img.FileData.Picture
Set img = Nothing

End Sub

查看更多
登录 后发表回答