How can i modify this code to display the results in a userform listbox?
Code
Dim rng As Range
Dim rngFirstOccurrence As Range
Dim lngColumns As Long
Dim strMessage As String
Dim varSearchValue
varSearchValue = TextBox1.Text
On Error Resume Next
Set rngFirstOccurrence = Range("e7:e1500").Find(What:=varSearchValue) ', LookAt:=xlwhole
Debug.Print rngFirstOccurrence.Address
If Not rngFirstOccurrence Is Nothing Then
For lngColumns = -5 To 6 'This will show the previous 3, current and next 3 cells of the row
strMessage = strMessage & rngFirstOccurrence.Offset(, lngColumns).Value & vbCrLf
Next lngColumns
MsgBox strMessage
strMessage = vbNullString
Else
MsgBox "No Match Found!"
GoTo ExitSub
End If
Do
Set rng = Nothing
Set rng = Range("e7:e1500").FindNext(rngFirstOccurrence)
If rng.Row <= rngFirstOccurrence.Row Then
Exit Do
End If
Debug.Print rng.Address
Err.Clear: On Error GoTo -1: On Error GoTo 0
If Not rng Is Nothing Then
Set rngFirstOccurrence = Nothing
Set rngFirstOccurrence = rng
For lngColumns = -5 To 6 'This will show the previous 3, current and next 3 cells of the row
strMessage = strMessage & rng.Offset(, lngColumns).Value & vbCrLf
Next lngColumns
MsgBox strMessage
strMessage = vbNullString
End If
Loop Until rng Is Nothing
ExitSub:
Set rng = Nothing
lngColumns = Empty
strMessage = vbNullString
Set rngFirstOccurrence = Nothing
End
Display More
This code works perfectly and displays the results in a message box. But for my new database im working on i need the results in a list box. Thanks in advance for any help.