简体   繁体   English

Excel VBA中的嵌套循环中的下一个没有错误

[英]Next Without For error in nested loop in Excel VBA

I am trying to figure out a way to run a Vlookup on a Cell in my "System File" by checking a table in a "New Data" File. 我试图通过检查“新数据”文件中的表来找出在“系统文件”中的单元格上运行Vlookup的方法。 HOWEVER, if there is an #N/A error, I want the cells' values to be unchanged. 但是,如果出现#N / A错误,我希望单元格的值保持不变。 I've come up with the following, however, I keep getting a "Next without For" error. 我提出了以下建议,但是,我一直收到“下一个没有For”的错误。 Is it possible to escape a nested For Next loop? 是否有可能摆脱嵌套的For Next循环?

The tl;dr semantic version: tl; dr语义版本:

   For i 1 to 10
       For j 1 to 3 
          Something with .Cells(i,j) 
          Set range X = .Find(thing
          If X = Nothing Then
            Next j *** <THIS IS WHERE MY ERROR IS THROWN
          Else
            -Do Something with X-
          End if
       Next j
   Next i

My more or less actual code is as follows: 我或多或少的实际代码如下:

Sub Thing()
    Dim SysWS As Worksheet
    Dim NewDataWS As Worksheet
        Dim NDSKUs As Range   ' This is set to the first column of the NewDataWS
        Dim NDMonthsRow As Range ' This is set to the first row of the NewDataWS      
    Dim SKU2look4 As String, Month2look4 As String        
        Dim ifoundtheSKU As Range 'the result of finding SKU2look4 inside of NDSKUs range
        Dim ifoundtheDate As Range 'the result of finding Month2look4 inside of NDMonthsRow range
    Dim i As Integer, j As Integer
    Dim workzone As Range 'The Cell being evaluated 
For i = 2 To SysWS.UsedRange.Columns.Count
  For j = 2 To SysWS.UsedRange.Rows.Count
     Set workzone = SysWS.Cells(j, i)
        SKU2look4 = SysWS.Cells(j, 1) 'SKUs are along the left column
        Month2look4 = SysWS.Cells(1, i) 'Dates are along the top row

'1-Find the right Date Column for extraction
    Set ifoundtheDate = NDMonthsRow.Find(What:=Month2look4, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
        If ifoundtheDate Is Nothing Then
                    Debug.Print (Month2look4 & " -Date NOT Found in New Date File")
                    ******Next j******
        Else
                    Debug.Print ("ifoundtheDate:" & ifoundtheDate.Address)
        End If
'2-Find the row
    Set ifoundtheSKU = NDSKUs.Find(What:=SKU2look4, LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
        If ifoundtheSKU Is Nothing Then
                    Debug.Print (SKU2look4 & " Not Found in the New Data File")
                    *********Next j******
            Else
                    Debug.Print ("ifoundtheSKU:" & ifoundtheSKU.Address)
        End If

'Set the "workzone" cell's value to that of the found row offset by the found column
                workzone = ifoundtheSKU.Offset(, (ifoundtheDate.Column - 1))
    Next j
Next i

Of course the ***s are not actually in there. 当然,***实际上不在其中。 Any thoughts on how I can accomplish this? 关于如何实现此目标的任何想法? Thanks in advance 提前致谢

For i = 1 to 10        
   For j = 1 to 3

     Something with .Cells(i,j) 

     Set rngX = .Find(thing)           
     If Not rngX Is Nothing Then

       Set rngY = .Find(thingelse)
       If Not rngY Is Nothing Then
          'something with rngX and rngY
       End If 

     End if

   Next j    
Next i

Use 采用

       For i=1 to 10
           For j=1 to 3 
              Something with .Cells(i,j) 
              Set range X = .Find(thing
              If X = Nothing Then
                Goto Nextj *** <THIS IS WHERE MY ERROR IS THROWN
              Else
                -Do Something with X-
              End if
NextJ:
           Next j
       Next i

Exit For会尽早终止当前的for循环(在您的情况下为内部循环)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM