循环遍历所有表单,最终得到错误

因此,我有一个工作簿,其中包含一个主表单和从属表单(除此之外)(因此select= 7),它将计算表单中数据部分的每行和每列的总计以及所有数据的总计。 我有这个部分的工作,但是当我继续下一步/select比较总表的总表与附属表总数之和(除了重新计算每张表的总数在变化的情况下或者一个没有填写等)我遇到了一个错误。

它在每一张纸上都能很好地执行总计function,然后在循环结束时会出现问题。 并非本工作手册中的每一张都是数据表,因此不会遵守其他人的具体布局。 由于这些没有特定的顺序,所以我在代码中跳过它们。 它通过有意识地捕捉由于不遵守别人的布局而产生的错误,然后跳到循环的底部来做到这一点。

目前我只有这些额外的表格之一,我试着把它拿出来,但是在循环结束时仍然有一个错误。 奇怪的是,即使我有一个error handling,它似乎并没有被抓住。 k这里是从属总数的总和,Position只是一个最大值(即使它们在其他function中不重要,我也想利用所有的variables),像MastLastRow – 6这样的奇数是考虑到标签和其他部分不是数据部分,标志部分是确保有人没有意外删除数据。 虽然我有多行转到ErrCatch7:这些只是帮助我跳到底部,应该出现在代码中的任何地方唯一的错误是当打一个额外的表。 如果你需要更多的澄清,在代码是做什么或需要我张贴的图片显示电子表格让我知道

ElseIf Fchoice = "7" Then k = 0 Application.Calculation = xlCalculationManual Application.ScreenUpdating = False Application.DisplayStatusBar = False On Error GoTo ErrCatch7: MastLastRow = Sheets("Master").Range("A7").CurrentRegion.Rows.Count For Each WSCount In Worksheets If WSCount.Name = "Master" Then MastLastCol = Sheets("Master").Range("A5").CurrentRegion.Columns.Count If Sheets("Master").Range("C7").Offset(MastLastRow - 7, 0) = "" Then Sheets("Master").Range("C7").Offset(MastLastRow - 7, 0).EntireRow.ClearContents End If MastLastRow = Sheets("Master").Range("A7").CurrentRegion.Rows.Count j = -1 i = 0 Set TempM = Range("H7") Do While i < MastLastRow - 6 Set TempP = Range(TempM.Offset(0, 1), TempM.Offset(0, MastLastCol)) TempM.Value = Application.Sum(TempP) Set TempM = TempM.Offset(1, 0) i = i + 1 Loop Do While j < MastLastCol - 8 Set TempP = Sheets("Master").Range(TempM.Offset(-1, 0), Sheets("Master").Range("I7").Offset(0, j)) TempM.Value = Application.Sum(TempP) Set TempM = TempM.Offset(0, 1) j = j + 1 Loop Sheets("Master").Range("H7").Offset(MastLastRow - 6, 0).Font.ColorIndex = 6 Sheets("Master").Range("H7").Offset(MastLastRow - 6, 0).Interior.Color = RGB(0, 0, 255) Sheets("Master").Range("H7").Offset(MastLastRow - 6, -1).Value = "Sheet Total" GoTo ErrCatch7: Else Set MastData = Sheets("Master").Range("A7") Set SubordData = WSCount.Range("A7") SubordLastRow = WSCount.Range("A7").CurrentRegion.Rows.Count SubordLastCol = WSCount.Range("A5").CurrentRegion.Columns.Count If WSCount.Range("C7").Offset(SubordLastRow - 7, 0) = "" Then WSCount.Range("C7").Offset(SubordLastRow - 7, 0).EntireRow.ClearContents End If SubordLastRow = WSCount.Range("A7").CurrentRegion.Rows.Count Flag = True If MastLastRow > SubordLastRow Then Position = MastLastRow Else Position = SubordLastRow End If 'Check if tasks in subordinate sheet have been mistakenly erased or misformated Do While i < Position And j < Position If MastData.Offset(i, 0).Value = SubordData.Offset(j, 0).Value Then i = i + 1 j = j + 1 ElseIf MastData.Offset(i, 0).Value = SubordData.Offset(j, 0).Value And MastData.Offset(i, 0).Value = vbNullString Then i = i + 1 j = j + 1 ElseIf MastData.Offset(i, 0).Value = vbNullString And SubordData.Offset(j, 0).Value <> vbNullString Then i = i + 1 ElseIf SubordData.Offset(j, 0).Value = vbNullString And MastData.Offset(i, 0).Value <> vbNullString Then j = j + 1 Else Flag = False Exit Do End If Loop If Flag = False Then MsgBox (OrgName + " appears to be missing tasks. This sheet will be ignored. Check for mistakes and compare totals later") GoTo ErrCatch7: End If j = -1 i = 0 Set TempM = WSCount.Range("H7") Do While i < SubordLastRow - 6 Set TempP = WSCount.Range(TempM.Offset(0, 1), TempM.Offset(0, SubordLastCol)) TempM.Value = Application.Sum(TempP) Set TempM = TempM.Offset(1, 0) i = i + 1 Loop Do While j < SubordLastCol - 8 Set TempP = WSCount.Range(TempM.Offset(-1, 0), WSCount.Range("I7").Offset(0, j)) TempM.Value = Application.Sum(TempP) Set TempM = TempM.Offset(0, 1) j = j + 1 Loop WSCount.Range("H7").Offset(SubordLastRow - 6, 0).Font.ColorIndex = 6 WSCount.Range("H7").Offset(SubordLastRow - 6, 0).Interior.Color = RGB(0, 0, 255) WSCount.Range("H7").Offset(SubordLastRow - 6, -1).Value = "Sheet Total" End If k = k + WSCount.Range("H7").Offset(SubordLastRow - 6, 0).Value ErrCatch7: Next Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Application.DisplayStatusBar = True MsgBox ("Master sheet hourly total = " + Sheets("Master").Range("H7").Offset(SubordLastRow - 6, 0).Value + ": Sum of subordinate sheets = " + k) 

我相信你的问题可能是因为你正在尝试使用ErrCatch7作为error handling程序。

我build议你用“On Error GoTo ErrCatch7Err”代替“On Error GoTo ErrCatch7”,然后在你的程序的最后部分(在一个Exit Sub或者Exit Function之后)包含一段:

 ErrCatch7Err: Resume ErrCatch7 

编辑:根据什么Maciej洛刚才说,这也是一个问题,你的error handling程序启用时,以外的For Next循环以及内部它。 我会build议你使用

  ... For Each WSCount In Worksheets On Error GoTo ErrCatch7Err ... ... (other code, including "GoTo ErrCatch7" statements if needed) ... ErrCatch7: Next On Error GoTo 0 ... Exit Sub ErrCatch7Err: Resume ErrCatch7 

如果你想要一个error handling程序来覆盖错误

 MastLastRow = Sheets("Master").Range("A7").CurrentRegion.Rows.Count 

语句在循环开始之前发生,使用不同的处理程序。 (无论如何你会希望执行不同的动作,所以使用相同的处理程序是没有意义的)

作为使用ErrCatch7作为On Error条件的目标和使用单独的标签作为目标的区别的例子,请考虑以下两个代码示例:

样品1:

 Sub TestMe() On Error GoTo ErrCatch7 For i = 1 To 10 MsgBox i j = i / 0 MsgBox j ErrCatch7: Next i End Sub 

样品2:

 Sub TestMe() On Error GoTo ErrCatch7 For i = 1 To 10 MsgBox i j = i / 0 MsgBox j ErrCatch7: Next i Exit Sub ErrCatch7Err: Resume ErrCatch7 End Sub 

示例1将显示数字1,然后(在仍处理“1/0”错误时)2,然后(尝试“2/0”时)给出除零错误的除法。

样本2将显示从1到10的所有数字。

MSDN文档指出:

你跳进For … Next循环的中间。 删除跳入循环。 不build议在For … Next循环中放置标签。

问题在这里:

  'before loop On Error GoTo ErrCatch7: MastLastRow = Sheets("Master").Range("A7").CurrentRegion.Rows.Count For Each WSCount In Worksheets .... 'jump into the loop is here ErrCatch7: Next 

解决您的问题最简单的方法是使用F8键debugging程序。

在这里和这里回答:

使用所示的代码,当您执行下一个语句时,您实际上仍被认为处于error handling例程中。

这意味着后续error handling程序是不允许的,直到你从当前的error handling程序恢复。

在你的情况下更好的架构会是这样的:

  'before loop On Error GoTo ErrCatch7 MastLastRow = Sheets("Master").Range("A7").CurrentRegion.Rows.Count For Each WSCount In Worksheets .... NextWorksheet: Next ' the rest of your code ErrCatch7: Resume NextWorksheet 

正如上面的答案中已经提到的,你可以在这个网站上find关于这个问题的很好的解释