2017-08-28 69 views
0

我有一個相當複雜的For循環與If聲明裏面和另一個For循環裏面。並給予一定的標準(即If InStr(1, q.Value, "Total"))在第二秒內我想結束整個If聲明並轉移到Next C結束如果/下一個C語句與For循環/ VBA

我明白,這是像洋蔥一樣分層,可能不是一個簡單的出路。

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..Then End If'實際上只是說'If ... Then:Do Nothing:End If',因爲'End If'只是'If'塊的最後一行。同樣,你的行'If ... Then _'也是有問題的,因爲那些是行連續標記。不要像那樣使用它們。它使你的代碼難以閱讀和調試。繼續下一個C的唯一方法是將你不想運行的代碼打包到if'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 identified」。有沒有一個快速解決這個問題,或者這可能是我的其他代碼更大的問題。如果'下一個q'線可能下移? – ShieldData

+0

你在哪裏定義了'q',並在哪一行發現錯誤?注意:不,下一個q不應該下移。 – trincot

+0

如果InStr(1,q.Value,「Total」)= 0,我得到這一行上的錯誤然後'跳過,如果循環過早退出',並且我在代碼開始處定義了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 - 作爲一個2.步驟嘗試把'Option Explicit'放在代碼的頂部。就像'如果InStr(1,q.Value,「Total」)Then End If'不應該被允許編譯。 – Vityata

+0

我絕對會。 – ShieldData