English 中文(简体)
VBA: 跳出 lo
原标题:VBA: jumping out of a for loop

我如何实现以下目标?

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
最佳回答

这不是一个简单的流动控制案例吗? 我不理解需要特别关键词来解决这个问题:

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

http://www.un.org。 或者,在你的法典中:

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
问题回答

回答问题的另一半:

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

Next n

Aren t vb 关键词资本化?

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

      more code

Next n

关键词是:

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

它是一个老问题,但只是为了,在这一页的校正和完整,这是Goto可以使用而无需形成Spaghetti法典的情况之一。

该系统将VBA的排他性与几乎所有其他较新的方案拟订语言联系起来,并向VBA方案设计人提供Eric在另一个答复中描述的同样功能——但。 在VBA没有工作。

    For n = 1 To something
        If needToExitLoopEntirely Then Exit For
        If condition Then GoTo ContinueFor        
          more code        
          ...           
          more code
ContinueFor:  <=this is a label; 
    Next n




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签