For循环跳过值

问题描述:

我创建了一个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

假设(与提供的码行)

目的是在范围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