长时间潜伏,第一次海报。这个错误一直困扰着我好几个星期,所以我不得不求助于帮助。
我有一个excel add in,我已经开发了。添加有一个自定义右键单击菜单,它取代了标准的Excel单击右键菜单,取消了SheetBeforeRightClick。当您删除右键单击的行时,此操作非常正常。
例如。右键单击“Major Projects”工作表上的单元格E11以显示菜单。 菜单代码:
Private Sub SheetBeforeRightClick(ByVal sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim ClstrCmd As CommandBar
'Display cluster display option commands
On Error GoTo connerr
If ActiveSheet.Name = "Control" Then
Cancel = True
Set ClstrCmd = FinanceCreateSubMenuControlNew
ClstrCmd.ShowPopup
ElseIf ActiveSheet.Name = "Input" Then
Cancel = True
Set ClstrCmd = FinanceCreateSubMenuInputNew
ClstrCmd.ShowPopup
ElseIf ActiveSheet.Name = "Major Projects" Or ActiveSheet.Name = "Projects GP Report" Then
Cancel = True
Set ClstrCmd = FinanceCreateSubMenuProjectsNew
ClstrCmd.ShowPopup
Else
'No Menu for this sheet
End If
connerr:
End Sub
Function FinanceCreateSubMenuProjectsNew() As CommandBar
''Create some objects
Dim cb As CommandBar
Dim cbc As CommandBarControl
''Ensure our popup menu does not exist
FinanceDeleteCommandBar "FINANCE_PROJECTS"
''Add our popup menu to the CommandBars collection
Set cb = CommandBars.Add(Name:="FINANCE_PROJECTS", Position:=msoBarPopup, MenuBar:=False, Temporary:=False)
Set cbc = cb.Controls.Add
With cbc
.Caption = "Insert Seperator Row"
.OnAction = "InsertMajorProjectsSeperator"
End With
Set cbc = cb.Controls.Add
With cbc
.Caption = "Delete Seperator Row"
.OnAction = "DeleteMajorProjectsSeperator"
End With
Set FinanceCreateSubMenuProjectsNew = cb
End Function
从我的自定义菜单中选择“删除行”。这将调用Procedure Deleterow。
Sub DeleteRow()
Deleterow = ActiveCell.Row
If ActiveWorkbook.Worksheets("Major Projects").Range("A" & Deleterow) = "x" Then
ActiveWorkbook.Worksheets("Major Projects").Rows(Deleterow).EntireRow.Delete
Else
MsgBox "You haven't selected a seperator to delete"
End If
End Sub
删除所选行。删除行后,代码将回退到FinanceCreateSubMenuProjectsNew函数。然后到SheetBeforeRightClick程序完成。它在SheetBeforeRightClick过程中到达End Sub并给出424 - Object requred错误。没有调试选项,我不能错误捕获它,因为它看起来在End Sub之后出错。我只能假设它,因为您右键单击的单元格已被删除,系统已丢失目标。如果我加入 msgbox Target.address 我可以在该行发生错误。如果我 设置Target = activecell 修复该错误的msgbox之前,但它仍然在End Sub上出错。我需要重置一个System Target类型的变量吗?
甚至投入 表(“主要项目”)。范围(“a1”)。选择 没有用。
因为代码错误,Addin卸载。因此,用户需要重新启动插件才能再次使用任何功能。
真正的踢球者是,一旦我重新启动插件,我就可以删除行而不会出现错误。再次出现错误的唯一方法是完全关闭Excel并重新开始。仅关闭文件不会导致错误重新发生。
答案 0 :(得分:0)
我尝试了一些简单的东西并且没有编译(变量名称与Sub名称相同)。可能是您应该定义并使用Long类型变量lngDeleteRow
:
Sub DeleteRow()
Dim lngDeleteRow as Long
Debug.Print "ActiveWorkbook: " & ActiveCell.Worksheet.Parent.Name
Debug.Print "ActiveSheet: " & ActiveCell.Worksheet.Name
Debug.Print "ActiveCell: " & ActiveCell.Address
If Err.Number <> 0 Then
Debug.Print "DeleteRow() : ERR " & Err.Number & " " & Err.Description
Err.Clear
End If
lngDeleteRow = ActiveCell.Row
If ActiveWorkbook.Worksheets("Major Projects").Range("A" & lngDeleteRow) = "x" Then
ActiveWorkbook.Worksheets("Major Projects").Rows(lngDeleteRow).EntireRow.Delete
Else
MsgBox "You haven't selected a seperator to delete"
End If
End Sub
您可能还希望在实时代码中添加更多Debug.Print行以确定错误。当我遇到这样的情况时,我会在每个开始的Sub和函数中添加Debug.Print Now & vbTab & "Sub/Function name - Start"
,并在末尾添加一个“Finish”,这样我就可以在立即窗口中更准确地跟踪错误。
答案 1 :(得分:0)
答案最终使用Application.OnTime函数来调用删除功能。这允许删除与运行的其余代码分开发生。