我創建了一個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
你怎麼知道它是否跳過項目?隨着35個嵌套9個層次的循環,這是78,815,638,671,875次迭代。它可能只是沒有完成(我知道,因爲我們還沒有經歷過宇宙的熱死亡)。 – Comintern
@代碼是一團糟,但嵌套循環是有條件的,所以有可能整個代碼可能在合理的時間內執行。也就是說,當然幾乎可以肯定的是,無論OP做什麼(儘管這不是很明確),做一些不太複雜的方法。 –
在提供的代碼中,_values_的_search被限制在範圍'A1:AI1'中。這是你的意圖? – EEM