Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50117

Unable to Seach the Data by "Name" in UserForm

$
0
0
Hello,

I have a UserForm to Search Data from my worksheet (name "Payment Records")...In the UserForm I have got a ListBox to display searched data

and In UserForm I have a combo box which gives option to search data from worksheet either by NAME or ID and there is also a TextBox.

The problem is that If I select to search data either by ID or Name..It only search the data by ID.

I want when I select "Search data by Name" It should search the data by Name and When I select "Search data by ID" It should search the data by ID.

Here is my code

Code:

Private Sub Label15_Click()

End Sub

Private Sub ShowRecord_Click()
Col = ComboBox1.ListIndex + 1
        If Col = 0 Then
            MsgBox "Please choose a category."
            Exit Sub
        End If
       
      If TextBoxID.Text = "" Then
          MsgBox "Please enter a search term."
          TextBoxID.SetFocus
          Exit Sub
      End If

Dim FoundIt    As Range
    Dim Id          As Double
    Dim Rng        As Range
    Dim StartAddx  As String
    Dim Wks        As Worksheet
   
   
        Set Wks = Worksheets("Payment Records")
   
        Id = Val(TextBoxID.Value)
       
        Set Rng = Wks.Range("A7").CurrentRegion.Columns(2).Cells
       
        Set FoundIt = Rng.Find(Id, , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
       
        If FoundIt Is Nothing Then
           
            Exit Sub
        End If
       
            StartAddx = FoundIt.Address
           
            ListBox1.Clear
           
            Do
                Row = FoundIt.Row
                With ListBox1
                    .AddItem
                    .List(.ListCount - 1, 0) = Wks.Cells(Row, "H").Text
                    .List(.ListCount - 1, 1) = Wks.Cells(Row, "J").Text
                    .List(.ListCount - 1, 2) = Wks.Cells(Row, "K").Text
                    .List(.ListCount - 1, 3) = Wks.Cells(Row, "I").Text
                   
                   
                End With
                Set FoundIt = Rng.FindNext(FoundIt)
                If FoundIt Is Nothing Then Exit Do
                If FoundIt.Address = StartAddx Then Exit Do
            Loop
           
End Sub
Private Sub UserForm_Initialize()

    Dim ColumnWidths As String
   
        With ListBox1
            .ColumnCount = 4
            ColumnWidths = "118;50;85"

            .ColumnWidths = ColumnWidths
        End With
   
End Sub

Private Sub UserForm_Activate()
 
  With ComboBox1
    For I = 1 To 2
      .AddItem Cells(7, I)
    Next I
    .SetFocus
  End With
 
End Sub


Can anyone please complete the above code.

I will be very thankful If anyone could help me plz.

Regards
z-eighty2

Viewing all articles
Browse latest Browse all 50117