Excel Can';当以编程方式向工作表添加代码时,此时不能进入中断模式

Excel Can';当以编程方式向工作表添加代码时,此时不能进入中断模式,excel,vba,Excel,Vba,我正在使用VBA工具, 我试图以编程方式向工作表中添加代码 这是我的密码 Public Function Add_NodePairingCode(ByRef wb As Workbook) Dim xPro1 As VBProject Dim xCom1 As VBComponent Dim xMod1 As CodeModule Dim xLine As Long Sheets("Node Pairing").Activate Sheets("N

我正在使用VBA工具, 我试图以编程方式向工作表中添加代码 这是我的密码

Public Function Add_NodePairingCode(ByRef wb As Workbook)
    Dim xPro1 As VBProject
    Dim xCom1 As VBComponent
    Dim xMod1 As CodeModule
    Dim xLine As Long
    Sheets("Node Pairing").Activate
    Sheets("Node Pairing").Unprotect
    With wb
        Set xPro1 = wb.VBProject
        Set xCom1 = xPro1.VBComponents(ActiveSheet.CodeName)
        Set xMod1 = xCom1.CodeModule
        With xMod1
            xLine = .CreateEventProc("SelectionChange", "WorkSheet") 'Getting error at this line
            xLine = xLine + 1
            .InsertLines xLine, "   Application.DisplayAlerts = False "
            xLine = xLine + 1
            .InsertLines xLine, "   Dim KeyCells As Range "
            xLine = xLine + 1
            .InsertLines xLine, "   Dim WS_Count As Integer"
            xLine = xLine + 1
            .InsertLines xLine, "   Dim I As Integer"
            xLine = xLine + 1
            .InsertLines xLine, "   Dim lnRow As Long, lnCol As Long"
            xLine = xLine + 1
            .InsertLines xLine, "   lnRow = 2 "
            xLine = xLine + 1
            .InsertLines xLine, "   WS_Count = ActiveWorkbook.Worksheets.Count"
             xLine = xLine + 1
            .InsertLines xLine, "   lnCol = Sheets(""Node Pairing"").Cells(lnRow, 1).EntireRow.Find(What:=""Use For Mac"", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column"
            xLine = xLine + 1
            .InsertLines xLine, "   Set KeyCells = Range(Cells(2, lnCol), Cells(2, lnCol).End(xlDown))"
            xLine = xLine + 1
            .InsertLines xLine, "   If Not Application.Intersect(KeyCells, Range(Target.Address)) _"
             xLine = xLine + 1
            .InsertLines xLine, "       Is Nothing Then"
            xLine = xLine + 1
            .InsertLines xLine, "       For I = 1 To WS_Count"
            xLine = xLine + 1
            .InsertLines xLine, "           If ActiveWorkbook.Worksheets(I).Name = ""Mac Table"" Then"
            xLine = xLine + 1

            .InsertLines xLine, "               If MsgBox(""Changing Use For Mac flag, will delete Mac Tables, Do You Want to Continue?"", vbYesNo) = vbYes Then "
            xLine = xLine + 1
            .InsertLines xLine, "                   Sheets(""Mac Table"").Delete"
            xLine = xLine + 1
            .InsertLines xLine, "                   Exit Sub"
            xLine = xLine + 1
            .InsertLines xLine, "               Else"
            xLine = xLine + 1
            .InsertLines xLine, "                   Exit Sub"
            xLine = xLine + 1
            .InsertLines xLine, "               End If"
            xLine = xLine + 1
            .InsertLines xLine, "           End If"
            xLine = xLine + 1
            .InsertLines xLine, "       Next I"
            xLine = xLine + 1
            .InsertLines xLine, "   End If"
            xLine = xLine + 1
            .InsertLines xLine, "   Application.DisplayAlerts = True "
        End With
    End With
End Function
我不知道我做错了什么,我尝试了所有的方法,比如公开函数并执行它,我研究了stackoverflow的解决方案,我可以找到相关的问题,但找不到解决方案。
任何人都可以帮我做这个

我已经经历了所有的可能性, 我发现了错误发生的原因是

当我调用此特定函数时,我的excel工作表正在后台计算单元格中的一些值(excel公式),同时此函数试图在编译模式下自定义工作表代码,因此我的工具给出了
VBA
错误
此时无法进入中断模式

我发现解决方案是:我在母版纸上添加了saperate按钮,并向该按钮添加了相同的代码(功能)。 现在我可以完美地运行我的工具,没有任何错误


希望这能帮助某人:)

使用wb。。。以
结尾在代码中是无用的。尝试添加对“Microsoft Visual Basic for Applications Extensibility 5.3”的引用。@FaneDuru我已经用block注释了不需要的内容,已经添加了对“Microsoft Visual Basic for Applications Extensibility 5.3”的引用,但我仍然收到相同的错误,还有其他解决方案吗?有人能提供解决方案吗?您在指定行收到了什么错误?@FaneDuru我收到了此错误消息:此时无法进入中断模式(查询标题)