[英]If Else statement only working for the last row of data in Excel VBA Userform
I'm struggling with something that sounds very simple, but there's something wrong with my code.我正在为听起来很简单的事情而苦苦挣扎,但我的代码有问题。
I have a userform with 2 textboxes when I type a 'serial number' in textbox 1, the 'RMA Number' in textbox 2 auto populates if the serial number matches an existing field in the 'RMA' column in the sheet.当我在文本框 1 中键入“序列号”时,我有一个带有 2 个文本框的用户表单,如果序列号与工作表中“RMA”列中的现有字段匹配,则文本框 2 中的“RMA 编号”会自动填充。
If it does not match I want textbox2 to clear up or say "No Match"如果不匹配,我希望 textbox2 清除或说“不匹配”
I did the If-Then-Else type of code but it seems to work only for the very last entry at the moment...我做了 If-Then-Else 类型的代码,但它似乎只适用于目前的最后一个条目......
What do I need to change in my code so it can match all the entries AND clear up when the Serial Number does not match??我需要在我的代码中更改什么,以便它可以匹配所有条目并在序列号不匹配时清除?
'Autopopulate RMA# with Serial Number
Private Sub SN_TextBox1_Change()
Dim serial1_id As String
serial1_id = UCase(Trim(SN_TextBox1.Text))
lastrow = Worksheets("RMA Tracker").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastrow
If UCase(Worksheets("RMA Tracker").Cells(i, 4).Value) = serial1_id Then
RMA_TextBox1.Text = Worksheets("RMA Tracker").Cells(i, 1).Value
Else
RMA_TextBox1.Value = ""
End If
Next i
End Sub
I think you can use Find()
method to server your purpose.我认为您可以使用Find()
方法来实现您的目的。 Below code will find TextBox1
value from RMA column (D:D)
.下面的代码将从 RMA 列(D:D)
中找到TextBox1
值。 If match found then it will return value from Column A:A
for matching row to TextBox2
.如果找到匹配项,那么它将从Column A:A
返回值以匹配行到TextBox2
。 If there is no match the it will show No Match
message to TextBox2
.如果没有匹配,它将向TextBox2
显示No Match
消息。
Private Sub CommandButton1_Click()
Dim RMA As String
Dim Rng As Range
RMA = Me.TextBox1
If Trim(RMA) <> "" Then
With Sheets("RMA Tracker").Range("D:D") 'D:D for column 4
Set Rng = .Find(What:=RMA, _
After:=.Range("A1"), _
Lookat:=xlWhole, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not Rng Is Nothing Then
Me.TextBox2 = Rng.Offset(0, -3)
Else
Me.TextBox2 = "No Match"
End If
End With
End If
End Sub
MultiLine
to True
in the properties of RMA_TextBox1
.要显示多个结果,您必须在RMA_TextBox1
的属性中将MultiLine
设置为True
。The Code编码
Private Sub SN_TextBox1_Change()
Const wsName As String = "RMA Tracker"
Const FirstRow As Long = 1
Const RMACol As Variant = "A"
Const IdCol As Variant = "D"
Const IfNot As String = "No Match"
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, IdCol).End(xlUp).Row
Dim SerialID As String: SerialID = Trim(SN_TextBox1.Value)
Dim i As Long, Result As String
For i = FirstRow To LastRow
If StrComp(ws.Cells(i, IdCol).Value, SerialID, vbTextCompare) = 0 Then
If Result <> "" Then
Result = Result & vbLf & ws.Cells(i, RMACol).Value
Else
Result = ws.Cells(i, RMACol).Value
End If
End If
Next i
If Result <> "" Then
RMA_TextBox1.Value = Result
Else
RMA_TextBox1.Value = IfNot
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.