VBA exit退出循环和函数

        VBA退出循环用Exit语句,在循环中使用Exit语句相当于C/C++的break语句,而在函数中使用Exit语句则相当于C/C++的return语句。

        一、退出循环。exit for退出for循环,exit do用于退出do 或 do while循环:

'获取非空行
Function GetNotNullRow(ByVal iStartRow, ByRef iRow)
    Dim Rng As Range
    For Each Rng In Range("B" & iStartRow & ":" & "P" & 100)
    If Rng <> "" Then
        iRow = Rng.Row
        Exit For
    End If
    Next
End Function

        二、退出函数。exit sub退出过程,exit function 退出function:

'获取源工作簿名
Sub GetSourceWorkbookName(strPath As String)
    Dim strName As String
    Dim iCount As Integer
    iCount = 0
    strName = Dir(strPath & "*.xls*")
    While Len(strName) > 0
        If strName <> strMainWorkbookName Then
            ReDim Preserve arrSourceWorkbookName(iCount)
            arrSourceWorkbookName(iCount) = strName
            strName = Dir()
            iCount = iCount + 1
        Else
            Exit Sub
        End If
    Wend
End Sub


猜你喜欢

转载自blog.csdn.net/gordennizaicunzai/article/details/79439780
vba
今日推荐