我在以下代码中获得了Object Global of Object Global失败错误。代码应该将所有列表框重置回原始值。 如果字段已更改,则代码可以正常工作,然后用户决定在保存之前返回到工作表以清除字段。但是,如果用户在保存后打开文件并决定清除字段,则会给出1004错误。
有谁能告诉我如何修复错误? 感谢
Sub Clear_Staff_Click()
Dim warning
warning = MsgBox(" !!!! WARNING !!!! " & vbNewLine & " You are about to ERASE all Staff " & vbNewLine & " Are you sure you want to" & vbNewLine & vbNewLine & " Erase ALL staff and start over?", vbOKCancel, "!!!!!WARNING !!!!!")
If warning = vbCancel Then Exit Sub
Dim rngLists As Range
Dim ListCell As Range
ActiveSheet.Unprotect "ABCD"
On Error Resume Next
Set rngLists = Sheets("Staff").UsedRange.SpecialCells(xlCellTypeAllValidation)
On Error GoTo 0
If Not rngLists Is Nothing Then
For Each ListCell In rngLists.Cells
ListCell.Value = Range(Trim(Mid(Replace(ListCell.Validation.Formula1, ":", String(99, " ")), 2, 99))).Value
Next ListCell
End If
Range("G7").ClearContents
Range("G10:G15").ClearContents
ActiveSheet.Protect "ABCD"
End Sub
标记为错误的行是:
ListCell.Value = Range(Trim(Mid(Replace(ListCell.Validation.Formula1, ":", String(99, " ")), 2, 99))).Value