gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:28:42 27 4
gpt4 key购买 nike

如果“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 的情况下,您缺少错误处理在列表中找不到值。此外,无需选择行来删除它们。

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

关于excel - 如果在需要字符串的地方提供了数字,则类型不匹配 - Excel VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53927361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com