2

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.

If it does not match I want textbox2 to clear up or say "No Match"

I did the If-Then-Else type of code but it seems to work only for the very last entry at the moment...

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


Mari2212
  • 25
  • 4

2 Answers2

0

I think you can use Find() method to server your purpose. Below code will find TextBox1 value from RMA column (D:D). If match found then it will return value from Column A:A for matching row to TextBox2. If there is no match the it will show No Match message to TextBox2.

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

enter image description here

Harun24hr
  • 30,391
  • 4
  • 21
  • 36
0

Lookup Value in UserForm TextBox

  • To show multiple results, you have to set MultiLine to True in the properties of RMA_TextBox1.

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
VBasic2008
  • 44,888
  • 5
  • 17
  • 28