如果在需要字符串的地方提供了数字,则类型不匹配 - Excel VBA

Type mismatch if Number provided where string is required - Excel VBA

如果 "ComboBox1.Value" 中的值是一个数字,则存在 运行-时间错误“13”:类型不匹配

我已经使用Cstr()将其转换为字符串,但仍然显示错误

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False

Dim WS As Worksheet, NROW As Variant
Set WS = ThisWorkbook.Sheets("ClientList")
Dim Q As Integer

NROW = Application.Match(ComboBox1.Value, WS.Range("A:A"), 0)
    ActiveSheet.Range("A" & NROW).Select
    Q = MsgBox("Do you want to delete this record", vbQuestion + vbYesNo)

    If Q = vbYes Then
        ActiveCell.EntireRow.Delete
        MsgBox "RECORD DELETED"
    Else
        'do nothing
    End If
Application.ScreenUpdating = True
End Sub

如果Application.Match无法在列表中找到值,您将缺少错误处理。此外,无需 select 行即可删除它们。

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False

Dim WS As Worksheet, NROW As Long
Set WS = ThisWorkbook.Sheets("ClientList")
Dim Q As Integer

If Not IsError(Application.Match(ComboBox1.Value, WS.Range("A:A"), 0)) Then

    Q = MsgBox("Do you want to delete this record", vbQuestion + vbYesNo)

    If Q = vbYes Then
        NROW = Application.Match(ComboBox1.Value, WS.Range("A:A"), 0)
        WS.Range("A" & NROW).EntireRow.Delete
        MsgBox "RECORD DELETED"
    Else
        'do nothing
    End If
End If

Application.ScreenUpdating = True
End Sub

工作表函数

Private Sub CommandButton1_Click()

  Application.ScreenUpdating = False

  Dim WS As Worksheet, NROW As Long
  Set WS = ThisWorkbook.Sheets("ClientList")
  Dim Q As Integer

  If IsError(WorksheetFunction.Match(ComboBox1.Value, WS.Range("A:A"), 0)) _
          Then Exit Sub
  NROW = WorksheetFunction.Match(ComboBox1.Value, WS.Range("A:A"), 0)

  Q = MsgBox("Do you want to delete this record", vbQuestion + vbYesNo)
  If Q = vbYes Then
      ActiveSheet.Range("A" & NROW).EntireRow.Delete
      MsgBox "RECORD DELETED"
    'Else
      ' do nothing
  End If

  Application.ScreenUpdating = True

End Sub