【问题标题】:EXCEL VBA WorksheetFunction.CountIf() in a SELECT CASESELECT CASE 中的 EXCEL VBA WorksheetFunction.CountIf()
【发布时间】:2013-02-18 19:17:36
【问题描述】:

我知道可以使用If 语句,但出于好奇,如标题中所述,是否可以使用SELECT 语句来执行以下BOLDED 的操作?我已将我的整个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"

案例是 "" & 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"

案例是 "" & WorksheetFunction.CountIf(Worksheets("CustomerList").Range("B:B"), response)

    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

【问题讨论】:

    标签: excel vba select case


    【解决方案1】:

    像这样?

    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

    下面的方法很有用,因为

    1. 当您查看您的代码时(比如说可能在一年之后)并且您确切地知道代码被注释后发生了什么。
    2. 易于维护代码。例如,如果工作表名称更改,则您只需在一处更改。另一种方法是也使用Codenames
    3. 您可以在所有 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
    

    【讨论】:

    • 非常感谢,它就像一个魅力!顺便问一下,您认为这部分代码效率更高吗? If 声明中的 SELECT 声明。
    • 我实际上会以稍微不同的方式做到这一点:)
    • 我可以按照你的方法吗?它们在性能方面是否具有可比性?
    • 我不会使用 Call addNewCust_Click 两次,我宁愿使用 Do Loop 并将 response 声明为变体 :)
    • 使用循环会更慢不是吗?
    猜你喜欢
    • 2010-12-06
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多