Selecting data containing cells in a row using custom word search function

AndyT88

New Member
Joined
Dec 26, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
I have the following piece of code that searches for custom searchwords in a spreadsheet and then highlights the rows.

How do I adjust it so it only selects the data present within those rows?




VBA Code:
    Dim Rng As Range, myUnion As Range, searchString As String
    Dim myCell As Range, arrSrc, El
    
    Set Rng = Selection
    If TypeName(Rng) <> "Range" Then MsgBox "You must select a range...", vbCritical, "Wrong selection": Exit Sub
    
   searchString = InputBox("Please type 'Sample' & any other searchwords seperated by a comma")
   searchString = Replace(searchString, ", ", ",")
  
   arrSrc = Split(searchString, ",")
   For Each El In arrSrc
        For Each myCell In Rng
            If InStr(myCell.Text, El) Then
                 If Not myUnion Is Nothing Then
                     Set myUnion = Union(myUnion, myCell.End(xlToRight.Select))
                 Else
                     Set myUnion = myCell.EntireRow
                 End If
            End If
        Next
  Next El
  If myUnion Is Nothing Then
       MsgBox "The text was not found in the selection"
   Else
       myUnion.Select
   End If
'
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this:

VBA Code:
Sub SearchData()
  Dim Rng As Range, myUnion As Range, searchString As String
  Dim myCell As Range, arrSrc, El
  
  Set Rng = Selection
  If TypeName(Rng) <> "Range" Then
    MsgBox "You must select a range...", vbCritical, "Wrong selection"
    Exit Sub
  End If
  
  searchString = InputBox("Please type 'Sample' & any other searchwords seperated by a comma")
  searchString = Replace(searchString, ", ", ",")
  
  arrSrc = Split(searchString, ",")
  For Each El In arrSrc
    For Each myCell In Rng
      If InStr(myCell.Text, El) Then
        If Not myUnion Is Nothing Then
          Set myUnion = Union(myUnion, myCell)
        Else
          Set myUnion = myCell
        End If
      End If
    Next
  Next El
  If myUnion Is Nothing Then
    MsgBox "The text was not found in the selection"
  Else
    myUnion.Select
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,391
Messages
6,124,673
Members
449,178
Latest member
Emilou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top