2012-02-10 15 views
0

"新しいデータ"ファイルのテーブルをチェックして、 "システムファイル"内のセルでVlookupを実行する方法を見つけようとしています。しかし、#N/Aエラーがある場合は、セルの値を変更しないでください。私は次のことを思いついたが、私は "次の理由なし"のエラーが出てくる。ネストされたFor Nextループからエスケープすることは可能ですか?次へなしExcel VBAのネストループでエラーが発生しました

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 

マイ多かれ少なかれ実際のコードは次のとおりです。もちろん

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 

*** sがそこに実際にはありません。どのように私はこれを達成することができますか?事前に おかげで(あなたのケースで、内側1)初期のループの電流を終了するために

答えて

7
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 
+1

+1。これは.FINDを使用するときの正しいアプローチです –

+0

+1ベストアプローチ – brettdj

+0

このアプローチは間違いなく機能しましたので、どうもありがとう!私は、NOT Nothing = Trueに対するテストのロジックフローが控えめであると感じていると思います。 –

1

使用

 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 
+0

私は、「Goto」ステートメントを使用するのが貧弱だと思っていましたか?同時に、これは私が考えていた論理フローを可能にする別のアプローチです。 –

+1

私のための良い練習は動作するものです:)この場合、うまくいくでしょう。 –

1

終了します。

+0

彼はforループを終了したくないので、次の繰り返しにスキップしたいと思っています。 – mischab1

関連する問題