VBA错误:为什么我的代码保持跳回行?

我为代码转储道歉,但经过两天的debugging,我觉得我已经开始失去它了,而且我越来越绝望了。 我已经开发了如下所示的代码

intColumnCount = wsStaff.Cells(1, Columns.Count).End(xlToLeft).Column intColumnLoop = 2 intStaffCount = 0 wsDisplay.Range("A2").EntireRow.UnMerge wsDisplay.Range("A2").EntireRow.Value = "" Do intRowCount = wsStaff.Cells(Rows.Count, intColumnLoop).End(xlUp).Row intRowLoop = 2 Do If IsEmpty(wsStaff.Cells(intRowLoop, intColumnLoop)) And intRowLoop <> 2 Then wsStaff.Range(wsStaff.Cells(intRowLoop, intColumnLoop).Address).Delete Shift:=xlUp intRowCount = wsStaff.Cells(Rows.Count, intColumnLoop).End(xlUp).Row Else intStaffCount = intStaffCount + 1 If wsDisplay.Cells(1, intStaffCount + 2).Value <> wsStaff.Cells(intRowLoop, intColumnLoop).Value And wsDisplay.Cells(1, intStaffCount + 2).Value <> wsStaff.Cells(intRowLoop + 1, intColumnLoop).Value And wsDisplay.Cells(1, intStaffCount + 2).Value <> wsStaff.Cells(2, intColumnLoop + 1).Value And wsDisplay.Cells(1, intStaffCount + 2).Value <> "" Then wsDisplay.Range(wsDisplay.Cells(1, intStaffCount + 2).Address).EntireColumn.Delete Shift:=xlToLeft intRowCount = wsStaff.Cells(Rows.Count, intColumnLoop).End(xlUp).Row intStaffCount = intStaffCount - 1 ElseIf wsDisplay.Cells(1, intStaffCount + 2).Value = wsStaff.Cells(intRowLoop + 1, intColumnLoop).Value Or wsDisplay.Cells(1, intStaffCount + 2).Value = wsStaff.Cells(2, intColumnLoop + 1).Value Or wsDisplay.Cells(1, intStaffCount + 2).Value = "" Then wsDisplay.Range(wsDisplay.Cells(1, intStaffCount + 2).Address).EntireColumn.Insert Shift:=xlToRight wsDisplay.Cells(1, intStaffCount + 2).Value = wsStaff.Cells(intRowLoop, intColumnLoop).Value intRowLoop = intRowLoop + 1 Else intRowLoop = intRowLoop + 1 End If If wsDisplay.Cells(1, intStaffCount + 2).Value = wsDisplay.Cells(1, intStaffCount + 1).Value Then wsDisplay.Range(wsDisplay.Cells(1, intStaffCount + 2).Address).EntireColumn.Delete Shift:=xlToLeft intRowCount = wsStaff.Cells(Rows.Count, intColumnLoop).End(xlUp).Row intStaffCount = intStaffCount - 1 End If wsDisplay.Cells(1, intStaffCount + 2).Interior.Color = RGB(255 - (0.1 * (255 - intColourPalette(intColumnLoop Mod 6 + 1, 1))), 255 - (0.1 * (255 - intColourPalette(intColumnLoop Mod 6 + 1, 2))), 255 - (0.1 * (255 - intColourPalette(intColumnLoop Mod 6 + 1, 3)))) End If Loop While Not intRowLoop > intRowCount wsDisplay.Range(wsDisplay.Cells(2, 4 + intStaffCount - intRowCount).Address, wsDisplay.Cells(2, 2 + intStaffCount).Address).Merge wsDisplay.Range(wsDisplay.Cells(2, 4 + intStaffCount - intRowCount).Address).Interior.Color = RGB(intColourPalette(intColumnLoop Mod 6 + 1, 1), intColourPalette(intColumnLoop Mod 6 + 1, 2), intColourPalette(intColumnLoop Mod 6 + 1, 3)) wsDisplay.Cells(2, 4 + intStaffCount - intRowCount).Value = wsStaff.Cells(1, intColumnLoop).Value wsDisplay.Cells(2, 4 + intStaffCount - intRowCount).Font.Bold = True wsDisplay.Cells(2, 4 + intStaffCount - intRowCount).Font.Color = RGB(255, 255, 255) intColumnLoop = intColumnLoop + 1 Loop While Not intColumnLoop > intColumnCount wsDisplay.Cells(1, 1).EntireRow.Orientation = -45 wsDisplay.Cells(1, 1).EntireRow.HorizontalAlignment = xlRight wsDisplay.Range(wsDisplay.Cells(1, 3), wsDisplay.Cells(2, intStaffCount + 2)).Borders.LineStyle = xlContinuous intDisplayRowLength = wsDisplay.Cells(1, Columns.Count).End(xlToLeft).Column intEraser = intStaffCount + 3 wsDisplay.Range(wsDisplay.Cells(1, intEraser), wsDisplay.Cells(1, intDisplayRowLength)).EntireColumn.Delete Shift:=xlToLeft End Sub 

所以我有两个问题,都涉及代码跳回行和重新执行代码。 我通过在不同的variables条件下几乎所有的断点上的代码遍历代码数百次来解决这个问题。

每当intRowCount被重新定义时,代码经常会跳回到开始,这个错误对输出没有害处,但是确实会大大增加计算时间。 我知道这可能是内置到Do循环,所以不方便,但不是最大的问题。

最大的问题是第二行,删除所有不必要的列后,代码将跳回到最后一个结束语句,这是在嵌套的do循环内。 我不知道为什么它跳回来,最重要的是,似乎intRowCount,intRowLoop,intColumnCount和intColumnLoopvariables更改为允许重复循环的代码。

这是如此灾难性的原因是因为intStaffCount不会改变,这意味着数据被添加了两次。

如果任何人都可以提供任何见解,将不胜感激。

编辑:它跳到代码中的位置,添加一个新的项目,并select无限地做到这一点,只要到达第二个最后一行。

编辑2:它实际上是达到了结束小组线,它只是没有结束

编辑3:在显示的代码外部发现解决scheme,事件必须在子被调用并在之后重新启用之前被禁用

问题不在于我的模块代码,而是在事件代码中,通过在程序被调用时禁用事件,问题得到解决。 谢谢你的解决scheme。