SELECT CASE 中的 EXCEL VBA WorksheetFunction.CountIf()

标签 excel vba select case

我知道可以使用If声明,但出于好奇,如标题中所述,是否可以使用 SELECT声明做某事为粗体 以下?我已经提交了我的全部 Sub如下以便更好地理解:

子 addNewCust_Click()

Dim response As String

response = Application.InputBox(prompt:="", Title:="New customer name", Type:=2)

Select Case response
Case False
    Exit Sub

'Check if response is not an empty value AND record found in "CustomerList"

Case Is <> "" & WorksheetFunction.CountIf(Worksheets("CustomerList").Range("B:B"), response) > 0


    MsgBox "'" & response & "' already exists on this sheet."
    Call addNewCust_Click

'Check if response is not an empty value and record is not found in "Customerlist"

Case Is <> "" & WorksheetFunction.CountIf(Worksheets("CustomerList").Range("B:B"), response) < 1


    Sheets("CustomerList").Range("B1048576").End(xlUp).Offset(1, 0).Value = response
    MsgBox "'" & response & "' successfully entered!"**

Case Else
        MsgBox "Field is empty!"
        Call addNewCust_Click

End Select

End Sub

最佳答案

像这样?

Sub addNewCust_Click()
    Dim response As String

    response = Application.InputBox(prompt:="", Title:="New customer name", Type:=2)

    Select Case response
    Case False: Exit Sub    
    'Check if response is not an empty value AND record found in "CustomerList"
    Case Is <> ""
        If WorksheetFunction.CountIf(Worksheets("CustomerList").Range("B:B"), response) > 0 Then
            MsgBox "'" & response & "' already exists on this sheet."
            Call addNewCust_Click
        Else
            Sheets("CustomerList").Range("B1048576").End(xlUp).Offset(1, 0).Value = response
            MsgBox "'" & response & "' successfully entered!"
        End If
    Case Else
        MsgBox "Field is empty!"
        Call addNewCust_Click
    End Select
End Sub

跟进(来自评论)
Select Case被认为比 If-Endif 快但是对于这么小的场景,效率比较是徒劳的。更重要的是如何编写代码

下面是另一种方式。我喜欢这种方式,因为事情被分解成更小的部分,并且一切都被正确地声明了。我没有触及下面的错误处理。详情见这里analysis .

下面的方法很有用,因为
  • 当您查看您的代码时(比如说可能在一年之后)并且您确切地知道代码被注释后发生了什么。
  • 易于维护代码。例如,如果工作表名称更改,则您只需在一处更改。另一种方法是也使用 Codenames
  • 您可以在所有 Excel 平台上使用相同的代码。如果您对您的范围进行硬编码,例如:Range("B1048576")那么上述代码在 Excel 2003 中将无法使用。

  • 示例代码
    Sub addNewCust_Click()
        Dim ws As Worksheet
        Dim Lrow As Long
        Dim response
    
        '~~> Set the relevant worksheet
        Set ws = ThisWorkbook.Worksheets("CustomerList")
    
        With ws
            Do
                '~~> Get user response
                response = Application.InputBox(prompt:="", Title:="New customer name", Type:=2)
    
                Select Case response
                    Case False: Exit Sub    '<~~ If user presses cancel or closes using 'X'
                    Case "": MsgBox "Field is empty!" '<~~ If user enters a blank entry
                    Case Else
                        '~~> Check if the entry exists
                        If WorksheetFunction.CountIf(.Range("B:B"), response) > 0 Then
                            MsgBox "'" & response & "' already exists on this sheet."
                        Else
                            '~~> Get last Row
                            Lrow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
                            '~~> Add the new entry
                            .Range("B" & Lrow).Value = response
                            MsgBox "'" & response & "' successfully entered!"
                            Exit Do 'OR Exit Sub (As Applicable)
                        End If
                End Select
            Loop
        End With
    End Sub
    

    关于SELECT CASE 中的 EXCEL VBA WorksheetFunction.CountIf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943721/

    相关文章:

    excel - 如何阻止excel运行所有错误

    vba - 具有多个条件都等于相同值的 IF 语句的替代方案

    SQL计算大多数书籍的作者

    jquery 选择两个非兄弟元素之间的元素

    c# - 如何使用C#为Excel创建 "Text Contains"格式条件(格式条件)

    excel - 将矩阵定义从 Excel 导入 MATLAB

    vba - 自定义数字格式以显示 0000.00

    excel - 如何永久添加新的多页选项卡并为其命名?

    即使编码了密码后,Excel VBA 仍然提示输入密码

    html - 如何使用 Css 修复此 "Select"下拉菜单