对象variables或块variables未设置,与我的错误转到

所以我有代码循环通过一个工作表(DataWS)的每一行,并使用其他工作表来查找string,如果没有findstring,那么我想代码去的地方,错误告诉它。 所以如果在工作表上找不到这个零件,那就去(某处)。 如果在工作表上findstring,则更新DataWS或稍后在代码中使用find的variables来引用在其他工作表中find的行。

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”语法不起作用? 我无法弄清楚为什么。 另外,有没有更好的办法做到这一点,除了我有?

如果我能进一步解释什么,请让我知道,我试图给你的整个画面以及代码分离出我在这些问题。

 PN_SO_NotFound: Sheets(DataWS).Cells(x, DSupplySourcecol) = "N/A" GoTo Updated 

这个。 您处于error handling子例程中 – VBA运行时处于错误状态,并且您正在跳转到所有位置。

当执行到达PN_SO_NotFound后的Updated标签时,VBA 仍处于错误状态,因为您从不重置它。 所以就它理解你的代码而言, Updated标签只是你的error handling子程序的另一部分。

虽然它认为它仍然在处理运行时错误,但它不会注意它遇到的On Error语句,因为它已经处于错误状态

replaceGoTo UpdatedResume Updated和,老实说, 手指交叉 ,应该“工作”。

但绝对要考虑一些严重的重构。 这样的意大利面代码在没有一系列严重的重构的情况下不会很容易debugging:当总共有0个GoTo跳转并且所有的过程合并到一个单独的屏幕中时,单一的东西。

而不是只使用On Error GoTo 0使用On Error GoTo 0 : On Error Goto -1来重置exception处理程序和错误状态。