【发布时间】:2018-09-05 15:55:26
【问题描述】:
我正在使用 VBA 从 SAP 中提取数据,并根据字符串是否包含某些单词来分离状态。当发生错误时,我需要它退出循环,因为这意味着 SAP 接口中没有更多数据,并且它正在尝试提取不存在的东西。我之前在这个宏中使用过这个没有问题,但是现在它的行为就像 on error goto 命令没有激活,而是弹出错误消息 -2147024809。
这是错误 goto 正常工作的第一个部分
'Total Number in lot loop
'========================================================
For C = 0 To 20
'Exits loop when no more are found in column
On Error GoTo No_More_In_Lot
'Use Required end column since there is always data here (C does not include the current QN)
place_holder = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").getcellvalue(C, "LTRMN") ' LTRMN=Name of column in recording
Notification_Status = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").getcellvalue(C, "STTXT")
'Exits when no more data
If place_holder = "" Then
Exit For
Else
End If
'Counts Each valid complaint
If InStr(1, Notification_Status, "BIMP") Then
'Do nothing
Else
If InStr(1, Notification_Status, "CC04") Then
'do nothing
Else
Number_In_Lot = Number_In_Lot + 1
End If
End If
Next C
No_More_In_Lot:
'Places # in lot in excel
Cells(i + 2, 8) = Number_In_Lot
在下面,我有以下代码,错误时 goto 无法正常工作。
有人看到我为此编写的代码有什么问题吗?
'Begins separating the notification statuses
'========================================================
Number_Substantiated = 0
Number_Not_Substantiated = 0
Number_Counterfeit = 0
Number_Cancelled = 0
Current_QN = Cells(i + 2, 2)
For C = 0 To 20
'Resets the Notification Status
Notification_Status = ""
'Exits loop when no more are found in column
On Error GoTo Skip
Notification_Number = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").getcellvalue(C, "QMNUM")
If Current_QN = Notificaiton_Number Then
'Do nothing (To not include the current notification status in the report)
Else
'Pull Notification status
Notification_Status = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").getcellvalue(C, "STTXT") ' STTXT=Name of column in recording
If InStr(1, Notification_Status, "CC01") Then
Number_Substantiated = Number_Substantiated + 1
Else: End If
'Include the notifcation status in the report
If InStr(1, Notification_Status, "CC02") Then
Number_Not_Substantiated = Number_Not_Substantiated + 1
Else: End If
'Include the notifcation status in the report
If InStr(1, Notification_Status, "CC03") Then
Number_Counterfeit = Number_Counterfeit + 1
Else: End If
'Include the notifcation status in the report
If InStr(1, Notification_Status, "CC04") Then
Number_Cancelled = Number_Cancelled + 1
Else: End If
End If
Next C
Skip:
'Passes values to sheet
Cells(i + 2, 9) = Number_Substantiated
Cells(i + 2, 10) = Number_Not_Substantiated
Cells(i + 2, 11) = Number_Counterfeit
Cells(i + 2, 12) = Number_Cancelled
【问题讨论】:
-
错误消息表明您正在做一些无效的事情 - 这几乎可以是任何事情。首先要尝试的是看看您是否可以按照您在以
Current_QN =开头的行中的方式设置单元格值。 -
是的,无效的是我试图在找不到要抓取的对象时提取数据(列尾)问题是我故意使用它来在出现错误时停止操作到达了。我已添加到我的原始帖子中,并在我首先提供的代码上方包含了该部分。它在不同的循环中使用相同的方法。如果没有 on error goto,它会给我同样的错误,但是当我使用 on error goto 时,它可以正常工作。
-
好的。下一步是将您在此处发布的代码缩减为仍然会产生问题的最小示例。
-
您没有正确使用
On Error GoTo No_More_In_Lot- 当它跳转到No_More_In_Lot标签时,它不会清除错误标志。您应该在代码末尾有错误处理程序 - 在Exit Sub之后和End Sub之前处理错误后,您可以Resume No_More_In_Lot。Skip也是如此 -
您不需要写
Else:End If- 只需End If即可。还有ElseIf,而不是在Else之后嵌入另一个If。