目前无法以编程方式将代码添加到工作表时进入中断模式

时间:2019-12-06 07:57:29

标签: 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("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以寻求解决方案,我能够找到相关的问题,但是找不到解决方案。 任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

我经历了所有的可能性, 我发现了为什么会发生错误,原因是,

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

我找到的解决方案是:我已在主表上添加了saperate按钮,并向该按钮添加了相同的代码(功能)。 现在我可以完美地运行我的工具了,

希望这会对某人有所帮助:)