删除复选框时共享工作簿错误

时间:2018-01-24 13:43:57

标签: excel vba excel-vba loops runtime-error

有人可以告诉我为什么以下代码运行正常,直到我分享工作簿?它一直运行到删除复选框的位置。我无法弄清楚发生了什么。据我所知,我没有使用任何不受支持的功能。

我得到了

  

运行时错误1004

删除复选框时

   Private Sub RunMe()

    Const BOX_SIZE As Integer = 16
    Dim ws As Worksheet
    Dim cell As Range
    Dim cbox As CheckBox
    Dim i As Integer, j As Integer
    Dim boxLeft As Double, boxTop As Double

    'Select Worsheet
    Set ws = ThisWorkbook.Worksheets("Sunday")

    'Clear Ranges that will be changed when CheckBoxes are clicked
    Worksheets("Sunday").Range("F2:G116").ClearContents
    Worksheets("Sunday").Range("I2:K116").ClearContents
    'Call FillDown

    'Delete checkboxes
    For Each cbox In ws.CheckBoxes
        cbox.Delete
    Next

    'Add checkboxes
    For i = 2 To 116
        For j = 8 To 8
            Set cell = ws.cells(i, j)
            With cell
                boxLeft = .Width / 2 - BOX_SIZE / 2 + .Left
                boxTop = .Height / 2 - BOX_SIZE / 2 + .Top
            End With
            Set cbox = ws.CheckBoxes.Add(boxLeft, boxTop, BOX_SIZE, BOX_SIZE)
            With cbox
                .Name = "CB" & i & j
                .Caption = ""
                .OnAction = "CheckBox_Clicked"
                .Placement = xlFreeFloating
            End With
        Next
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

我最终通过使工作簿独占然后在完成添加按钮后再次共享工作簿来解决此错误。

Sub MakeExclusive()

    If ActiveWorkbook.MultiUserEditing Then
        Application.DisplayAlerts = False
        ActiveWorkbook.ExclusiveAccess
        Application.DisplayAlerts = True
    End If

End Sub

Sub MakeShared()

    If Not ActiveWorkbook.MultiUserEditing Then
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs ActiveWorkbook.Name, accessmode:=xlShared
        Application.DisplayAlerts = True
    End If

End Sub

call Sub MakeExclusive()在我的sub的第一行和我call MakeShared()的最后一行。