2017-03-09 154 views
-2

我創建了一個for循環來查找Sheet1單元格中的值並將它們複製/粘貼到Sheet2。問題是,如果這個值不在那裏,它就會回到循環列表中。例如,一旦我到達「Anna」,因爲她不在Sheet1中,它會一直回到Jerry。我希望它從傑裏到約翰。如果安娜不存在,那就跳過她。For循環跳過值

Sheets("Sheet1").Select 

     For b = 1 To 35 

      If Cells(1, b) = "Jerry" Then 
          Columns(b).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("A:A").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 

Sheets("Sheet1").Select 

     For c = 1 To 35 

      If Cells(1, c) = "Bob" Then 
          Columns(c).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("B:B").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 

Sheets("Sheet1").Select 

     For d = 1 To 35 

      If Cells(1, d) = "Larry" Then 
          Columns(d).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("C:C").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 

Sheets("Sheet1").Select 

     For e = 1 To 35 

      If Cells(1, e) = "Steve" Then 
          Columns(e).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("E:E").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 


Sheets("Sheet1").Select 

     For f = 1 To 35 

      If Cells(1, f) = "Wilson" Then 
          Columns(f).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("I:I").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 


Sheets("Sheet1").Select 

     For g = 1 To 35 

      If Cells(1, g) = "Anna" Then 

          Columns(g).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("P:P").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 


Sheets("Sheet1").Select 

     For h = 1 To 35 

      If Cells(1, h) = "Kevin" Then 
          Columns(h).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("Q:Q").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 

Sheets("Sheet1").Select 

     For i = 1 To 35 

      If Cells(1, i) = "Gary" Then 
          Columns(i).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("X:X").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 


Sheets("Sheet1").Select 

     For j = 1 To 35 

      If Cells(1, j) = "John" Then 
          Columns(j).Select 
          Selection.Copy 
          Sheets("Sheet2").Select 
          Columns("R:R").Select 
          Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
          :=False, Transpose:=False 
          Application.CutCopyMode = False 

          Sheets("Sheet1").Select 

End If 
Next j 
End If 
Next i 
End If 
Next h 
End If 
Next g 
End If 
Next f 
End If 
Next e 
End If 
Next d 
End If 
Next c 
End If 
Next b 
+5

你怎麼知道它是否跳過項目?隨着35個嵌套9個層次的循環,這是78,815,638,671,875次迭代。它可能只是沒有完成(我知道,因爲我們還沒有經歷過宇宙的熱死亡)。 – Comintern

+0

@代碼是一團糟,但嵌套循環是有條件的,所以有可能整個代碼可能在合理的時間內執行。也就是說,當然幾乎可以肯定的是,無論OP做什麼(儘管這不是很明確),做一些不太複雜的方法。 –

+0

在提供的代碼中,_values_的_search被限制在範圍'A1:AI1'中。這是你的意圖? – EEM

回答

1

假設(與提供的碼行)

目的是在範圍A1:AI1搜索值的列表Sheet1(目標範圍),當該值是發現將找到該值的整個列複製到Sheet2中的相應列。列表中的值都不應該在目標範圍內重複,如果是這樣的話,只有第一次出現纔會起作用。

以下代碼使用數組來保存要找到的值列表以及在Sheet2中執行復制的列。它遍歷值列表,然後遍歷目標範圍,找到該值後,執行副本並退出第二個循環,以避免覆蓋之前找到的值。

Sub For_And_Copy() 
Dim aValCol As Variant 
aValCol = [{"Jerry","A:A";"Bob","B:B";"Larry","C:C";"Steve","E:E";"Wilson","I:I";"Anna","P:P";"Kevin","Q:Q";"Gary","X:X";"John","R:R"}] 
Dim Wsh1 As Worksheet, Wsh2 As Worksheet 
Dim b1 As Byte, b2 As Byte 
    With ThisWorkbook 
     Set Wsh1 = .Sheets("Sheet1") 
     Set Wsh2 = .Sheets("Sheet2") 
    End With 
    For b1 = 1 To UBound(aValCol) 
     For b2 = 1 To 35 
      If Wsh1.Cells(1, b2).Value2 = aValCol(b1, 1) Then 
       Wsh1.Columns(b2).Copy 
       Wsh2.Columns(aValCol(b1, 2)).PasteSpecial Paste:=xlPasteValues 
       Application.CutCopyMode = False 
       Exit For 'Exits the b2 For...Next so values found are copied only once 
    End If: Next: Next 
End Sub 

關於您的代碼,只會告訴您它有太多不必要的嵌套For..Next。閱讀建議的頁面,比較提供給您的代碼。讓我知道您可能與代碼有關的任何問題。

推薦閱讀以下網頁獲得的資源有了更深的瞭解使用:

Excel ObjectsUsing ArraysWith StatementFor...Next Statement