Advertisement
YasserKhalil2019

T4121_Display Search Results In ListBox Capture Row In Sheet

Oct 13th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. https://excel-egy.com/forum/t4121
  2. ----------------------------------
  3.  
  4. Dim ws As Worksheet
  5. Const colCount As Integer = 21
  6.  
  7. Private Sub UserForm_Initialize()
  8. Set ws = ThisWorkbook.Worksheets("Sheet1")
  9. End Sub
  10.  
  11. Private Sub TextBox1_Change()
  12. Dim a, ary(), txt As String, r As Long, rr As Long, c As Long, cc As Long
  13.  
  14. With ws
  15. a = .Range("A2:T" & ws.Cells(Rows.Count, 1).End(xlUp).Row).Value
  16. txt = Me.TextBox1
  17. Me.ListBox1.Clear
  18.  
  19. For r = 1 To UBound(a, 1)
  20. If InStr(1, .Cells(r + 1, "A"), txt, vbTextCompare) = 1 Then
  21. rr = rr + 1
  22. ReDim Preserve ary(1 To colCount, 1 To rr)
  23. For c = 1 To colCount
  24. cc = Choose(c, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)
  25. If cc = 21 Then
  26. ary(c, rr) = .Cells(r + 1, cc).Row
  27. Else
  28. ary(c, rr) = .Cells(r + 1, cc).Value
  29. End If
  30. Next c
  31. End If
  32. Next r
  33. End With
  34.  
  35. If rr Then Me.ListBox1.Column = ary
  36. Erase ary
  37.  
  38. If TextBox1.Text = "" Then ListBox1.Clear
  39. End Sub
  40.  
  41. Private Sub ListBox1_Click()
  42. Dim myRow As Long
  43.  
  44. ws.Cells.Interior.ColorIndex = 0
  45.  
  46. If Me.ListBox1.ListIndex <> -1 Then
  47. Me.Top = 300
  48. myRow = Me.ListBox1.List(Me.ListBox1.ListIndex, 20)
  49. ws.Rows(myRow).EntireRow.Interior.ColorIndex = 6
  50. Application.Goto ws.Range("A" & myRow), True
  51. End If
  52. End Sub
  53.  
  54. Private Sub CommandButton1_Click()
  55. ws.Cells.Interior.ColorIndex = 0
  56. Unload Me
  57. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement