2

我知道可以使用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"

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

1 回答 1

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但是对于这么小的场景,效率比较是徒劳的。更重要的是如何编写代码

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

下面的方法很有用,因为

  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
于 2013-02-18T19:25:47.043 回答