excel - 此时无法进入中断模式,同时以编程方式将代码添加到工作表
问题描述
我正在使用 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
错误Can't Enter Break mode at this time
我发现解决方案是:我在主工作表上添加了单独的按钮,并向该按钮添加了相同的代码(功能)。现在我可以完美地运行我的工具而不会出现任何错误,
希望这会对某人有所帮助:)
推荐阅读
- django - 在 django 中创建标签并根据它们进行过滤
- javascript - 在 JavaScript 中使用 execcommand 函数时可以删除第一个字符吗?
- mysql - AWS:使用 IAM 作为身份验证方法从 Lambda Java 脚本连接到 Aurora
- azure - Azure 应用服务 - VNet 集成 vs 专用终结点 vs 混合连接
- r - 在 r 中创建自定义图例
- javascript - TensorflowJS 库在本地主机上工作,但一旦我部署到 Netlify 就不再工作?
- python - python 正则表达式以任何顺序匹配 A 但不匹配 B
- python-3.x - 在 python 中将 SILK 编码数据转换为 wav 文件
- spring-boot - 需要覆盖 jhipster 使用的 spring boot 版本
- docker - ReadyAPI docker 错误,许可证无效:无法获得浮动许可证。原因:无法为浮动许可证服务器创建 HTTPS 服务