excel - excel VBA查找不适用于特定值
问题描述
Find 功能效果很好,但我不明白的例外情况很少。我有用户表单,我使用 find 方法通过其代码获取有关产品/项目的所有信息,并在用户表单中按下按钮后显示它。我表中的产品代码由这样的代码组成:1230、1231、1232...1239。主要问题是我不明白为什么像:1-9、123 这样的数字不会触发消息框“找不到产品”?
Private Sub btnSearch_Click()
Dim i As Long
Dim totalRows As Long
Dim itemCode As Range
Set itemCode = ThisWorkbook.Sheets("Data").Range("A:A").Find(Me.txtCode.Value)
totalRows = Worksheets("Data").Range("A:A").CurrentRegion.Rows.Count
'searching by code
If Trim(Me.txtCode.Value) = "" Then
Me.txtCode.SetFocus
MsgBox "Need item code"
Exit Sub
End If
If itemCode Is Nothing Then
MsgBox "Can't find product with such code"
End If
For i = 2 To totalRows
If Trim(Cells(i, 1)) = Trim(Me.txtCode) Then
txtName.Text = Cells(i, 2)
'unit of measurement name
txtUnitName.Text = Cells(i, 3)
txtPrice.Text = Cells(i, 4)
Exit For
End If
Next i
End Sub
解决方案
代替:
Set itemCode = ThisWorkbook.Sheets("Data").Range("A:A").Find(Me.txtCode.Value)
和:
Set itemCode = ThisWorkbook.Sheets("Data").Range("A:A").Find(Trim(Me.txtCode.Value), LookIn:=xlValues, LookAt:=xlWhole)
推荐阅读
- reactjs - I got undefined data while getting it from useSelector in react
- c++ - (为什么不)在定义时初始化 struct val
- c++ - 为什么没有声明从基模板类继承的成员函数?
- python - 拆分数据集后如何绘制带有色调的pairplot
- python - 当我在 DataFrame 中使用双括号时会发生什么?
- python - 使用 youtube.search().list 时获取相同的 youtube 频道 ID
- python - 有没有办法“关闭”或降级使用英特尔数学内核库(mkl)并使用“原始”(非 mkl)Numpy 函数?
- javascript - 如何使用 discord.js 跟踪谁邀请了机器人?
- reactjs - 我可以更改 MUI 中标签的位置吗?
- jenkins - 从一个 git 存储库拉到另一个 git 存储库时排除管道文件(例如 Jenkinsfile、bitbucket-pipelines.yml)