所以我有一个包含主表和从属表的工作簿以及其他内容(因此选择= 7件)它计算工作表数据部分的每一行和每一列的总数以及总数所有数据。我有这个部分工作,但后来当我继续下一步/选项比较主表的总和与从属表的总和之和(添加重新计算每个表的总数,如果有变化或者一个没有填写等)我遇到了错误。
它在每张工作表上执行总计功能,然后它似乎在循环结束时遇到问题。并非本工作簿中的每个工作表都是数据表,因此不符合其他工作表的特定布局。由于这些没有任何特定的顺序,我在我的代码中只是跳过它们。它通过有意识地捕获因不使用其他人的布局然后跳到循环底部而产生的错误来实现这一点。
目前我只有其中一张额外的纸张,我尝试将其取出但是在循环结束时仍然会出现错误。奇怪的是,即使我有错误处理,它似乎也没有被它捕获。 k这里是从属总数的总和,位置只是一个最大值(我想利用我的所有变量,即使它们在其他功能中不重要),任何奇数如MastLastRow - 6都要考虑到标签和工作表的其他部分不是数据部分,标志部分确保有人不会意外删除数据。虽然我在GoTo 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)
答案 0 :(得分:3)
我相信您的问题可能是因为您尝试将ErrCatch7用作错误处理程序。
我建议你更换&#34; On Error GoTo ErrCatch7&#34;使用&#34; On Error GoTo ErrCatch7Err&#34;,然后在程序的最后包含一个部分(在Exit Sub或Exit Function之后),其中包含:
ErrCatch7Err:
Resume ErrCatch7
编辑:基于Maciej Los刚才所说的,在For Next循环之外以及在其内部启用错误处理程序也是一个问题。我建议您使用
...
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
如果您希望错误处理程序覆盖
中的错误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,然后(同时仍然处理&#34; 1/0&#34;错误)2,然后(尝试&#34; 2/0&#34;)给出除法零错误。
样本2将显示1到10之间的所有数字。
答案 1 :(得分:2)
你跳进了For ... Next循环的中间。删除跳转到循环中。 不建议在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
密钥调试程序。
答案 2 :(得分:1)
使用显示的代码,您实际上仍然被认为是在其中 敲击下一个语句时的错误处理例程。
这意味着在您之前不允许使用后续错误处理程序 从当前的恢复。
在您的案例中,更好的架构将是:
'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
正如上面的答案中已经提到的,你可以在this site
中找到关于这个问题的一个很好的解释。