I tried to make a simple userform in Excel by VBA Code.
The main function looks for a number and displays the values associated with it.
For example:
If my input is 1 (TextBox), then it should find this: 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.
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. So, remove the OERN, return the match to a Variant and test for 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.