【问题标题】:VBA Exit Subroutine Command Within A Function函数内的VBA退出子程序命令
【发布时间】:2014-07-21 14:44:37
【问题描述】:

我有一个函数,其目的是接收一个字符串并在电子表格的第一行中搜索包含该字符串的单元格,如果这样做,它会选择该列并将其值分配给一个变量。但是,如果没有包含该字符串,我希望它完全退出子例程。我尝试在函数中抛出 Exit Sub 命令,但我不断收到错误消息。该函数永远不会由主子直接调用,而是由子调用的其他函数调用。有人对我如何使它起作用有任何想法吗?这是我的函数代码:

Function ColSearch(Heading As String) As Integer
'Determines the column number of the desired heading

Sheets("CS-CRM Raw Data").Select
Sheets("CS-CRM Raw Data").Unprotect

    On Error Resume Next
    If Sheets("CS-CRM Raw Data").Cells.Find(What:=Heading, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column > 0 Then
        myCol = Sheets("CS-CRM Raw Data").Cells.Find(What:=Heading, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column
    Else
        End
    End If

ColSearch = myCol

End Function

【问题讨论】:

  • 只是一个提示 - 当你写 On Error Resume Next 时,你还应该确保在下游某处写 On Error Goto 0On Error Goto <MyErrorLabel>

标签: excel vba


【解决方案1】:

您只需添加Exit Function 即可返回。

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2012-01-03
    • 1970-01-01
    • 2014-08-29
    • 2020-04-28
    相关资源
    最近更新 更多