[英]UserForm VBA Excel - Function Match
I tried to make a simple userform in Excel by VBA Code.我试图通过 VBA 代码在 Excel 中制作一个简单的用户表单。
The main function looks for a number and displays the values associated with it.主 function 查找一个数字并显示与其关联的值。
For example:例如:
If my input is 1 (TextBox), then it should find this: Worksheet "db", Cell(1, 2)如果我的输入是 1 (TextBox),那么它应该找到这个:Worksheet "db", Cell(1, 2)
this is the second day I have spent on this now and it is still not working.这是我现在花在这个上的第二天,它仍然无法正常工作。 I guess the problem is with the Range value.
我想问题出在 Range 值上。
My code:我的代码:
Option Explicit
Private Sub CommandButton1_Click()
Dim RecordRow As Long
Dim RecordRange As Range
On Error Resume Next
RecordRow = Application.Match(CLng(TextBox1.Value), Sheets("db").Range("B:B"), 0)
Set RecordRange = Sheets("db").Range("B:G").Cells(1, 1).Offset(RecordRow - 1, 0)
TextBox2.Value = RecordRange(1, 1).Offset(0, 1).Value
TextBox3.Value = RecordRange(1, 1).Offset(0, 2).Value
TextBox4.Value = RecordRange(1, 1).Offset(0, 3).Value
TextBox5.Value = RecordRange(1, 1).Offset(0, 4).Value
End Sub
Could you support me, please?请你支持我好吗?
If a match is not found Application.Match
doesn't through a runtime error, it returns a error value.如果未找到匹配项
Application.Match
未通过运行时错误,则返回错误值。 So, remove the OERN, return the match to a Variant and test for IsError
因此,删除 OERN,将匹配返回到 Variant 并测试
IsError
Private Sub CommandButton1_Click()
Dim RecordRow As Variant
RecordRow = Application.Match(CLng(TextBox1.Value), Sheets("db").Range("B:B"), 0)
If Not IsError(RecordRow) Then
With Sheets("db").Cells(RecordRow, 2)
TextBox2.Value = .Offset(0, 1).Value
TextBox3.Value = .Offset(0, 2).Value
TextBox4.Value = .Offset(0, 3).Value
TextBox5.Value = .Offset(0, 4).Value
End With
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.