excel - 多个 If/Then 调用选项

标签 excel vba if-statement subroutine

我有几个工作表。有 3 个基于工作表名称的子程序 CALL 选项。我已经尝试重新排列和 If、If Not、Else 和 ElseIf、Exit Sub、End If 等。如果 CALL 完成并返回,它不可避免地会到达最后一个 CALL(这里... IsolOther)并执行它甚至通过前一个调用有效。我还试图找出 SELECT CASE 函数用于多种选择。 CBI、Fire、LEA 和 InCase 的 CALL 可以完美运行。问题是当我有一个不同名称的工作表时......我希望 CALL 转到 IsolOther。

        Sub DetermineTabs()

        'DETERMINE WHICH SUBROUTINE TO CALL BASED ON SHEET (TAB) NAME

            Dim newTab As String
            newTab = ActiveSheet.Name

        'Call appropriate Category processing routine    
                    'Call IsolNamesOnly Subroutine if ActiveSheet is named ... CBI
                If newTab = "CBI" Then Call IsolNamesOnly Else
                    'Call IsolNamesOnly Subroutine if ActiveSheet is named ... Fire
                If newTab = "Fire" Then Call IsolNamesOnly Else
                    'Call IsolNamesOnly Subroutine if ActiveSheet is named ... LEA
                If newTab = "LEA" Then Call IsolNamesOnly Else
                    'Call IsolInCase Subroutine if ActiveSheet is named ... InCase
                If newTab = "InCase" Then Call IsolInCase Else
                   'Call IsolOther Subroutine if ActiveSheet is named ... anything else
                If newTab <> "CBI" Or newTab <> "Fire" Or newTab <> "LEA" Or newTab <> "InCase" Then Call IsolOther Else
        
        End Sub
我还尝试过“If Not newTab = "CBI"Or newTab = "Fire"Or newTab = "LEA"Or newTab = "InCase"Then Call IsolOther Else"(不带引号作为最后一个 IF 行的替代),以及将最后一行设为单独的 If Not/Else,但我失败了。如果前 4 张都没有找到,我想执行那个 CALL 和 End Sub。我查看了问题 60781118、51274411、24684092 和其他 Google 查询。帮助 。 . .请。

最佳答案

考虑SELECT...CASE并将多个条件与 Case Else健康)状况。

Select Case newTab
    Case "CBI", "LEA", "Fire"
    Call IsolNamesOnly 

    Case "InCase" 
    Call IsolInCase 

    Case Else
    Call IsolOther
End Select

关于excel - 多个 If/Then 调用选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70400476/

相关文章:

c# - 在 C# 中,我如何使用 ||我正在寻找 2 个文件的 if 语句中的操作数?

java - 简单的 if 语句似乎没有执行代码的某些部分

c# - 针对多个值测试变量?

java - 在 Excel 中是否有任何支持自动筛选的 Java-Excel 库?

excel - 如何从Excel列字母中获取列号(或索引)

vba - Do Loop 类型不匹配中的“If”语句

vba - 在 Excel VBA 中命名数组维度

javascript - 查找字符串中的重复项

asp.net - 当我单击 RadGrid 导出到 Excel 的按钮时没有任何反应

vba - 如何使这个 VBA 循环(对于每个单元格,复制粘贴)更快?