If Else 语句仅适用于 Excel VBA Userform 中的最后一行数据
If Else statement only working for the last row of data in Excel VBA Userform
我正在努力解决一些听起来很简单的事情,但我的代码有问题。
当我在文本框 1 中键入 'serial number' 时,我有一个带有 2 个文本框的用户表单,如果序列号与 [=25= 中的现有字段匹配,文本框 2 中的 'RMA Number' 会自动填充] 栏中的 sheet.
如果不匹配,我希望 textbox2 清除或说“不匹配”
我做了 If-Then-Else 类型的代码,但目前它似乎只对最后一个条目有效...
我的代码需要更改什么才能匹配所有条目并在序列号不匹配时清除??
'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
我认为您可以使用 Find()
方法来达到您的目的。下面的代码将从 RMA 列 (D:D)
中找到 TextBox1
值。如果找到匹配项,那么它将 return 值从 Column A:A
匹配到 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
在用户窗体文本框中查找值
- 要显示多个结果,您必须在
RMA_TextBox1
的属性中将 MultiLine
设置为 True
。
代码
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
我正在努力解决一些听起来很简单的事情,但我的代码有问题。
当我在文本框 1 中键入 'serial number' 时,我有一个带有 2 个文本框的用户表单,如果序列号与 [=25= 中的现有字段匹配,文本框 2 中的 'RMA Number' 会自动填充] 栏中的 sheet.
如果不匹配,我希望 textbox2 清除或说“不匹配”
我做了 If-Then-Else 类型的代码,但目前它似乎只对最后一个条目有效...
我的代码需要更改什么才能匹配所有条目并在序列号不匹配时清除??
'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
我认为您可以使用 Find()
方法来达到您的目的。下面的代码将从 RMA 列 (D:D)
中找到 TextBox1
值。如果找到匹配项,那么它将 return 值从 Column A:A
匹配到 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
在用户窗体文本框中查找值
- 要显示多个结果,您必须在
RMA_TextBox1
的属性中将MultiLine
设置为True
。
代码
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