2010-05-11 151 views
1

如何實現以下目標?VBA:跳出for循環

Sub Macro1() 
' 
' Macro1 Macro 
' 

' 
    Worksheets("Drop-down").Select 
    n = Cells(1, 1).End(xlDown).Row 
    For i = 1 To n 
     ActiveSheet.Cells(i, 2).Select 
     ******************************************************* 
     If Worksheets("Misc").Cells(2, i).Value = "" Then 
      continue i 
     End If 
     ******************************************************* 
     If Worksheets("Misc").Cells(3, i).Value <> "" Then 
      Set validationRange = Range(Worksheets("Misc").Cells(2, i), Worksheets("Misc").Cells(2, i).End(xlDown)) 
     Else 
      Set validationRange = Worksheets("Misc").Cells(2, i) 
     End If 
     With Selection.Validation 
      .Delete 
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
      xlBetween, Formula1:=validationRange.Address 
      .IgnoreBlank = True 
      .InCellDropdown = True 
      .InputTitle = "" 
      .ErrorTitle = "" 
      .InputMessage = "" 
      .ErrorMessage = "" 
      .ShowInput = True 
      .ShowError = True 
     End With 
    Next i 
End Sub 

回答

4

這不是一個簡單的流量控制的情況下?我不明白特殊的關鍵字來解決這個問題,需要:

For i = 0 To 10 
If Not condition Then 
    some other code 
Next 

編輯:或者,在你的代碼:

Sub Macro1() 
' 
' Macro1 Macro 
' 

' 
    Worksheets("Drop-down").Select 
    n = Cells(1, 1).End(xlDown).Row 
    For i = 1 To n 
     ActiveSheet.Cells(i, 2).Select 
     ******************************************************* 
     If Not Worksheets("Misc").Cells(2, i).Value = "" Then 
     ******************************************************* 
      If Worksheets("Misc").Cells(3, i).Value <> "" Then 
       Set validationRange = Range(Worksheets("Misc").Cells(2, i), Worksheets("Misc").Cells(2, i).End(xlDown)) 
      Else 
       Set validationRange = Worksheets("Misc").Cells(2, i) 
      End If 
      With Selection.Validation 
       .Delete 
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
       xlBetween, Formula1:=validationRange.Address 
       .IgnoreBlank = True 
       .InCellDropdown = True 
       .InputTitle = "" 
       .ErrorTitle = "" 
       .InputMessage = "" 
       .ErrorMessage = "" 
       .ShowInput = True 
       .ShowError = True 
      End With 
    ******** 
     End If 
    ******** 
    Next 
End Sub 
+0

謝謝。愚蠢的我。我想我不能再正確地編碼了...... – stanigator 2010-05-11 06:54:25

+0

不用擔心,很高興我能幫上忙。 – 2010-05-11 06:56:35

+0

這確實是這樣做的最好方法。幾個筆記,在VBA中,沒有'Next i'它只是下一個 – TerrorAustralis 2010-05-11 06:58:18

0

不是vb的關鍵字大寫?

For n = 1 To something 
    If condition Then 
     Continue For 
    End If 

    ' more code 

Next n 

Continue關鍵字做你想做的。

http://msdn.microsoft.com/en-us/library/801hyx6f(VS.80).aspx

+0

我認爲當你在IDE中是關鍵字自動大寫,但如果你只是寫一個VBScript文件,它會忽略這種情況。 – Tom 2010-05-11 06:21:06

+0

VBA IDE窗口無法識別您的建議。 – stanigator 2010-05-11 06:22:56

+0

以什麼方式?哪一部分? – Eric 2010-05-11 06:25:56

2

而要回答這個問題的另一半:

For n = 1 To something 
    If condition Then 
     Exit For 
    End If 
    ' more code 

Next n 
+0

這不會做我想要的,因爲它完全退出for循環。 – stanigator 2010-05-11 06:22:32

+1

您的問題被問及如何跳出「for循環」。從您的代碼中,您似乎想要的實際上是「繼續」聲明,正如@Eric回答的那樣,我爲完整性添加了「退出」。正如另一位回答者提到的那樣,使用其他語句也不錯。皮膚有很多種方法去皮膚。 – Tom 2010-05-11 07:00:12