我偶然发现了这个有用的VBA函数:Find a row from Excel table using VBA
Function GetRow(TableName As String, ColumnNum As Long, Key As Variant) As Range
On Error Resume Next
Set GetRow = Range(TableName) _
.Rows(WorksheetFunction.Match(Key, Range(TableName).Columns(ColumnNum), 0))
If Err.Number <> 0 Then
Err.Clear
Set GetRow = Nothing
End If
End Function
此功能对文本完全正常。但是我在搜索数字时遇到了严重的问题。在搜索整数时,我永远不会回来。
我正在使用这样的功能:
If Not Userform_Input.Text = "" Then
Set ref = GetRow("table", 3, Userform_Input.Text)
If Not ref Is Nothing Then
variable_a = Cells(ref.Row, 2).value
End If
End If
查看代码我看不出问题。还有其他人吗?
答案 0 :(得分:1)
您可以尝试更改以下代码:
If Not Userform_Input.Text = "" Then
Set ref = GetRow("table", 3, cint(Userform_Input.Text)) ' force numeric
If Not ref Is Nothing Then
variable_a = Cells(ref.Row, 2).value
End If
End If
也许这会有所帮助