SELECT CASE中的EXCEL VBA WorksheetFunction.CountIf()

时间:2013-02-18 19:17:37

标签: excel vba select case

我知道可以使用If声明,但出于好奇心,如标题中所述,是否可以使用SELECT语句执行 BOLDED 下面?为了更好的理解,我已经提交了我的全部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"
  

案例是<> “”& WorksheetFunction.CountIf(Worksheets(“CustomerList”)。范围(“B:B”),响应)> 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(工作表(“CustomerList”)。范围(“B:B”),响应)< 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

1 个答案:

答案 0 :(得分:2)

喜欢这个吗?

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中使用。
  4. 示例代码

    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