2017-08-28 8 views
0

Forループの場合はかなり複雑ですが、Ifステートメントが内部にあり、もう1つがForループ内にあります。そして、その2番目のForループの中に特定の基準(すなわち、If InStr(1, q.Value, "Total"))が与えられたら、Ifステートメント全体を終了してNext Cに移動したいと思います。End If/Next Forループ/ VBAを使用したCステートメント

私はこれがタマネギのように重なっていることを理解しています。簡単な方法がないかもしれません。

For Each C In copyRng 

     If IsNumeric(C) And C.Value <> "0" And Len(C) <> 0 And C.Value <> "2017" Then 

      Set rowRange = xSheet.Range(C, C.EntireColumn.Cells(1)) 'set range from cell up to the top cell of the comment/ Fix the 2017 thing 

      For Each q In rowRange 'Loop through that range and find the Account number just above it and set it as rowSrc 
       If InStr(1, q.Value, "C-") And Not ISIN(C, uniqueVal) Then Set rowSrc = q 
       If InStr(1, q.Value, "Total") Then End If 'At this point I want to leave the entire If Statement and move on to the next C 
      Next q 


      Set colSrc = C.EntireRow.Offset(0).Cells(1) 'find alert connected with the number 
      numCol = DestSh.Cells.Find(colSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column 'look for the column in which the same alert is listed 
      numRow = DestSh.Cells.Find(rowSrc.Value, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row 'look for row in which the same account is listed 

      'Set destination 
      Set destRng = DestSh.Cells(numRow, numCol) 

      'Copy to destination Range 
      C.Copy destRng 

     End If 

    Next C 
+1

'If'文はそのように動作いけません。あなたの行 'If..Then End If'は本当に' If ... Then:Do Nothing:End If'を 'End If'から' If'ブロックの最後の行だけと言っています。同様に、 'If ... Then _'という行は、行継続トークンであるため、問題があります。そのようなものは使わないでください。コードの読み込みやデバッグが難しくなります。次のCへ進む唯一の方法は、 'If Not Instr()Then'の逆に実行したくないコードをラップすることです。条件が満たされないので、次のCが実行されます。 –

+0

@BrandonBarneyは助けてくれてありがとう。私は本当にIfステートメントがどのように機能していたのかよく分かりませんでした。 – ShieldData

答えて

1

あなたはExit Forを必要として、ループの後、さらに別のIfブロック内の残りのコードを置く:

For Each C In copyRng 
    If IsNumeric(C) And C.Value <> "0" And Len(C) <> 0 And C.Value <> "2017" Then 
     Set rowRange = xSheet.Range(C, C.EntireColumn.Cells(1)) 

     For Each q In rowRange 
      If InStr(1, q.Value, "C-") And Not ISIN(C, uniqueVal) Then Set rowSrc = q 
      If InStr(1, q.Value, "Total") Then Exit For ' Exit loop prematurely 
     Next q 
     If q is Nothing Then ' Skip if loop was exited prematurely 
      Set colSrc = C.EntireRow.Offset(0).Cells(1) 
      numCol = DestSh.Cells.Find(colSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column 
      numRow = DestSh.Cells.Find(rowSrc.Value, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row 

      Set destRng = DestSh.Cells(numRow, numCol) 
      C.Copy destRng 
     End If 
    End If 
Next C 
+0

助けてくれてありがとう。 If文が私が想像していたものとは異なる働きをしていることを認識しています。私はあなたのコードを試してみることにしました。エラーメッセージ "Object variable or With-Variable not identify"を受け取りました。それは速い修正か、それは私のコードの残りの部分で大きな問題になる可能性があります。 '次のq'行を下に移動する必要がありますか? – ShieldData

+0

あなたは 'q'をどこで定義しましたか?どのラインでエラーを出していますか?注意:いいえ、「次へ」は下に移動しないでください。 – trincot

+0

InStr(1、q.Value、 "Total")= 0 Then 'ループが早期に終了した場合はスキップします。'というコードを範囲としてコードの先頭にqを定義しました。 – ShieldData

0

それぞれがThen後下線削除し、コード全体を書き換えます。ロジックが変更されます。あなたはそれをあなたが思うように他の人にはほとんど行っていません。

がこれを確認します。一般的には VBA - How the colon `:` works in VBA code with condition

を、あなたのコードは、あなたはそれがうまくいくと思うように動作しません。以下の項目をチェックしてください

Public Sub TestMe() 

    If 1 = 2 Then _ 
     Debug.Print "I am true - 1=2" 
     Debug.Print "I should be also true" 

    If 2 = 3 Then _ 
     Debug.Print "I am true 2=3" 

End Sub 
+1

ありがとうございました。私は、より洗練されたコードを保証するために、「Then _」のような自由な使用を止める必要があります。 – ShieldData

+0

@FSchildorfer - あなたのコードの先頭に 'Option Explicit'を入れてみましょう。 'If​​ InStr(1、q.Value、" Total ")If End Ifのようなものは'コンパイルすることを許されてはいけません。 – Vityata

+0

私は間違いありません。 – ShieldData

関連する問題