所以我的代码遍历一张纸上的每一行(DataWS)并使用其他工作表来查找字符串,如果找不到字符串,那么我希望代码转到On Error告诉它的地方。因此,如果在工作表上找不到该部件,则转到(某处)。 如果在工作表上找到字符串,则会更新DataWS,或者稍后在代码中使用找到的变量来引用在其他工作表中找到的行。
Sub MM011_to_Data()
Dim Dlr As Long, Dlc As Long, x As Long
Dim DataWS As String, MMWS As String, OmitWS As String
On Error GoTo DataSheetInvalid
Sheets("Data").Activate
DataWS = Sheets("Data").Name
On Error GoTo 0
On Error GoTo MM011SheetInvalid
MMWS = Sheets("MM011").Name
On Error GoTo 0
On Error GoTo OmitSheetInvalid
OmitWS = Sheets("Omit Parts").Name
On Error GoTo 0
'********************BELOW ARE VARIABLES IN THE DATA SHEET********************
Dim DStatuscol As Integer, DSupplySourcecol As Integer, DPOcol As Integer, DPOIcol As Integer, DPRcol As Integer
Dim DPLOcol As Integer, DWillShipcol As Integer, DSOLIcol As Integer, DRelPNcol As Integer
DStatuscol = Sheets(DataWS).Rows(1).Find("Status", lookat:=xlWhole).Column
DSupplySourcecol = Sheets(DataWS).Rows(1).Find("Supply Source", lookat:=xlWhole).Column
DPOcol = Sheets(DataWS).Rows(1).Find("PO#", lookat:=xlWhole).Column
DPOIcol = Sheets(DataWS).Rows(1).Find("PO Item", lookat:=xlWhole).Column
DPRcol = Sheets(DataWS).Rows(1).Find("PR#", lookat:=xlWhole).Column
DPLOcol = Sheets(DataWS).Rows(1).Find("PLO", lookat:=xlWhole).Column
DWillShipcol = Sheets(DataWS).Rows(1).Find("Will Ship", lookat:=xlWhole).Column
DSOLIcol = Sheets(DataWS).Rows(1).Find("CC SO/LI", lookat:=xlWhole).Column
DRelPNcol = Sheets(DataWS).Rows(1).Find("RELEASED PN", lookat:=xlWhole).Column
'********************BELOW ARE VARIABLES IN THE MM011 SHEET********************
Dim MMSupplySourcecol As Integer, MMInvcol As Integer, MMPOcol As Integer, MMPOIcol As Integer, MMPLOcol As Integer, MMPRcol As Integer
Dim MMPOShipDatecol As Integer, MMStatuscol As Integer, MMPNConcatcol As Integer
MMPNConcatcol = Sheets(MMWS).Rows(1).Find("PN Concatenate", lookat:=xlWhole).Column
MMSupplySourcecol = Sheets(MMWS).Rows(1).Find("Supply Source", lookat:=xlWhole).Column
MMInvcol = Sheets(MMWS).Rows(1).Find("Inventory Qty", lookat:=xlWhole).Column
MMPOcol = Sheets(MMWS).Rows(1).Find("PO Number", lookat:=xlWhole).Column
MMPOIcol = Sheets(MMWS).Rows(1).Find("PO Item", lookat:=xlWhole).Column
MMPLOcol = Sheets(MMWS).Rows(1).Find("Supply Work Order", lookat:=xlWhole).Column
MMPRcol = Sheets(MMWS).Rows(1).Find("Requisition Number", lookat:=xlWhole).Column
MMPOShipDatecol = Sheets(MMWS).Rows(1).Find("Supply Delivery Calendar Date", lookat:=xlWhole).Column
'********************BELOW ARE THE VARIABLES IN THE OMIT SHEET********************
Dim OPNcol As Integer, OPNrow As Long
OPNcol = Sheets(OmitWS).Rows(1).Find("Part Number", lookat:=xlWhole).Column
'********************LOOP TO UPDATE THE DATA SHEET********************
Dim DConcatPNSO As String, MMConcatrow As String
Dim SupplySource As String, PO_Number As String, PO_Item As String, PLO_WO As String, Requisition_Number As String, Supply_Del_Date As String
Dim DPartNumber As String
Dlr = Sheets(DataWS).Cells(Rows.Count, 1).End(xlUp).Row
Dlc = Sheets(DataWS).Cells(1, Columns.Count).End(xlToLeft).Column
For x = 2 To Dlr
'DConcatPNSO is the variable for the Data WS which concatenates the Released PN and the SO LI
DConcatPNSO = Sheets(DataWS).Cells(x, DRelPNcol) & " " & Sheets(DataWS).Cells(x, DSOLIcol)
DPartNumber = Sheets(DataWS).Cells(x, DRelPNcol)
'If there is no sales order then skip and say no sales order (NSO)
If Sheets(DataWS).Cells(x, DSOLIcol) = "" Then
Sheets(DataWS).Cells(x, DSupplySourcecol) = "NSO"
GoTo Updated
End If
'Checks to see if the part is on the Omit list
On Error GoTo NotOmit
OPNrow = Sheets(OmitWS).Columns(OPNcol).Find(What:=DPartNumber, lookat:=xlWhole).Row
On Error GoTo 0
If OPNrow > 0 Then
Cells(x, DSupplySourcecol) = "Omit"
GoTo Updated
End If
NotOmit:
On Error GoTo PN_SO_NotFound
MMConcatrow = Sheets(MMWS).Columns(MMPNConcatcol).Find(DConcatPNSO, lookat:=xlWhole).Row
On Error GoTo 0
'Hold all of the info from cells into variables
SupplySource = Sheets(MMWS).Cells(MMConcatrow, MMSupplySourcecol)
PO_Number = Sheets(MMWS).Cells(MMConcatrow, MMPOcol)
PO_Item = Sheets(MMWS).Cells(MMConcatrow, MMPOIcol)
PLO_WO = Sheets(MMWS).Cells(MMConcatrow, MMPLOcol)
Requisition_Number = Sheets(MMWS).Cells(MMConcatrow, MMPRcol)
Supply_Del_Date = Format(Sheets(MMWS).Cells(MMConcatrow, MMPOShipDatecol), "MM/DD/YYYY")
'If the Supply Source is PO then do the below
If SupplySource = "PO" Then
Sheets(DataWS).Cells(x, DStatuscol) = SupplySource & " " & Sheets(MMWS).Cells(MMConcatrow, MMPOcol) _
& " " & Sheets(MMWS).Cells(MMConcatrow, MMPOIcol) & " " & Format(Sheets(MMWS).Cells(MMConcatrow, MMPOShipDatecol), "MM/DD/YYYY")
Sheets(DataWS).Cells(x, DSupplySourcecol) = SupplySource
Sheets(DataWS).Cells(x, DPOcol) = Sheets(MMWS).Cells(MMConcatrow, MMPOcol)
Sheets(DataWS).Cells(x, DPOIcol) = Sheets(MMWS).Cells(MMConcatrow, MMPOIcol)
Sheets(DataWS).Cells(x, DWillShipcol) = Format(Sheets(MMWS).Cells(MMConcatrow, MMPOShipDatecol), "MM/DD/YYYY")
GoTo Updated
End If
'If the Supply Source is PLO then do the below
If SupplySource = "PLO" Then
Sheets(DataWS).Cells(x, DStatuscol) = SupplySource & " " & Sheets(MMWS).Cells(MMConcatrow, MMPLOcol)
Sheets(DataWS).Cells(x, DSupplySourcecol) = SupplySource
Sheets(DataWS).Cells(x, DPLOcol) = Sheets(MMWS).Cells(MMConcatrow, MMPLOcol)
GoTo Updated
End If
'If the Supply Source is PR then do the below
If SupplySource = "PR" Then
Sheets(DataWS).Cells(x, DStatuscol) = SupplySource & " " & Sheets(MMWS).Cells(MMConcatrow, MMPRcol)
Sheets(DataWS).Cells(x, DSupplySourcecol) = SupplySource
Sheets(DataWS).Cells(x, DPRcol) = Sheets(MMWS).Cells(MMConcatrow, MMPRcol)
GoTo Updated
End If
'If the Supply Source is BPL then do the below
If SupplySource = "BPL" Then
Sheets(DataWS).Cells(x, DStatuscol) = SupplySource & " " & "Borrow Payback Loan"
Sheets(DataWS).Cells(x, DSupplySourcecol) = SupplySource
GoTo Updated
End If
'If the Supply Source is QA then do the below
If SupplySource = "QA" Then
Sheets(DataWS).Cells(x, DStatuscol) = SupplySource & ", PO is " & Sheets(MMWS).Cells(MMConcatrow, MMPOcol) _
& " " & Sheets(MMWS).Cells(MMConcatrow, MMPOIcol) & " " & Format(Sheets(MMWS).Cells(MMConcatrow, MMPOShipDatecol), "MM/DD/YYYY")
Sheets(DataWS).Cells(x, DSupplySourcecol) = SupplySource
Sheets(DataWS).Cells(x, DPRcol) = Sheets(MMWS).Cells(MMConcatrow, MMPRcol)
GoTo Updated
End If
Updated:
Next x
Exit Sub
'****************ERROR HANDLING****************
DataSheetInvalid:
MsgBox "The worksheet with the MS2 All Open Order Report should be titled ""Data""." & vbCr & vbCr & "Please rename the worksheet and restart this sub.", vbCritical, "Worksheet Name"
Exit Sub
MM011SheetInvalid:
MsgBox "The worksheet with Cognos (MM011) data should be titled ""MM011""." & vbCr & vbCr & "Please rename the worksheet and restart this sub.", vbCritical, "Worksheet Name"
Exit Sub
OmitSheetInvalid:
MsgBox "The worksheet with Omit Parts data should be titled ""Omit Parts""." & vbCr & vbCr & "Please rename the worksheet and restart this sub.", vbCritical, "Worksheet Name"
Exit Sub
PN_SO_NotFound:
Sheets(DataWS).Cells(x, DSupplySourcecol) = "N/A"
GoTo Updated
End Sub
因此,在此代码中,持续获取运行时错误的部分是其中一个On Error处理程序:
'Checks to see if the part is on the Omit list
On Error GoTo NotOmit
OPNrow = Sheets(OmitWS).Columns(OPNcol).Find(What:=DPartNumber, lookat:=xlWhole).Row
On Error GoTo 0
If OPNrow > 0 Then
Cells(x, DSupplySourcecol) = "Omit"
GoTo Updated
End If
NotOmit:
On Error GoTo PN_SO_NotFound
MMConcatrow = Sheets(MMWS).Columns(MMPNConcatcol).Find(DConcatPNSO, lookat:=xlWhole).Row
On Error GoTo 0
在第一个循环中它们都可以工作,但是当它涉及到任何循环后,我得到了错误。
要修复这些错误,我使用了以下代码:
Set OPNrng = Sheets(OmitWS).Columns(OPNcol).Find(What:=DPartNumber, lookat:=xlWhole)
If OPNrng Is Nothing Then
GoTo NotOmit
Else
OPNrow = OPNrng.Row
End If
If OPNrow > 0 Then
Cells(x, DSupplySourcecol) = "Omit"
GoTo Updated
End If
NotOmit:
Set MMConcatrng = Sheets(MMWS).Columns(MMPNConcatcol).Find(DConcatPNSO, lookat:=xlWhole)
If MMConcatrng Is Nothing Then
GoTo PN_SO_NotFound
Else
MMConcatrow = MMConcatrng.Row
End If
有没有理由为什么" On Error GoTo"语法不起作用?我无法弄清楚为什么。 另外,除了我有什么更好的方法吗?
如果我可以进一步解释,请告诉我,我试图给你全面的图片以及我遇到这些问题的代码。
答案 0 :(得分:3)
PN_SO_NotFound:
Sheets(DataWS).Cells(x, DSupplySourcecol) = "N/A"
GoTo Updated
此。您正处于错误处理子例程中 - VBA运行时处于错误状态,并且您GoTo
- 跳到了整个地方。
在点击Updated
后执行转到PN_SO_NotFound
标签时,VBA 仍处于错误状态,因为您从未重置它。因此,只要它理解您的代码,Updated
标签就是您的错误处理子例程的另一部分。
虽然它认为它仍在处理运行时错误,但它不会注意它遇到的任何On Error
语句,因为它已经处于错误状态。
将GoTo Updated
替换为Resume Updated
,老实说,手指交叉,它应该“正常”。
但绝对要考虑一些严重的重构。像这样的意大利面条代码在没有一系列严重重构的情况下很快就会变得更容易调试:当你总共有0 GoTo
次跳跃并且所有程序都适合单个屏幕时,你就会知道你已经完成了,每个人做一件事。
答案 1 :(得分:1)
而不是仅使用On Error GoTo 0
使用On Error GoTo 0 : On Error Goto -1
的组合来重置异常处理程序和错误状态。