Copy or move rows that are highlighted based on conditions.

suzinger

New Member
Joined
Jan 31, 2017
Messages
16
Hello!

I moved a database from an old DOS-based program to an excel worksheet. I used conditional formatting so that someone can search all cells for any term and the row where that term is found is highlighted in yellow.

I would like to know how to do the following (if it is possible):
*Enter search term
*Rows with term in any cell are highlighted yellow. Table is 217 Rows x 6 Columns.
THEN -
*EITHER - 1. Automatically copy all the highlighted rows to another sheet OR 2. Move the highlighted rows to the top of the table.
*This sort/move will happen each time a new search terms is entered.

The end result I am trying to achieve is that someone can do a search and not have to scroll through the table to find the rows highlighted in yellow. I am open to any suggestions on how to achieve this goal.

Thank you for your time and assistance!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
This script will continue running as long as there are values in column "A" of sheet named "MyData"
The data needs to be in a sheet named "MyData"
It will copy the rows to Sheet named("New"). You will need to create this sheet and name it "New"

Code:
Sub Check_Rows()
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim Lastrowa As Long
Dim c As Range
Dim MyValue As String
MyValue = InputBox("Enter value to search for")
Lastrow = Sheets("MyData").Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = Sheets("New").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("MyData").Activate
    For i = 1 To Lastrow
        For Each c In Range("A" & i & ":F" & i)
            If c.Value = MyValue Then
                Rows(i).Copy Destination:=Sheets("New").Rows(Lastrowa)
                Rows(i).Cells.Interior.ColorIndex = 6
                Lastrowa = Lastrowa + 1
            End If
        Next
    Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Upvote 0
Thank you for the link! I knew how to install the Macro but didn't know how to run it.

Ok, so I made a Test workbook with your Macro. When I run the Macro, a small window opens that reads "Enter value to search for". Nothing happens after I enter my search term and click "Ok".

Do I need to make any changes to my table? Do I need to remove my conditional formatting? Right now my sheet looks like this:

 
Upvote 0
The script continues looking down column "A" till there is no more data in column "A"

It looks for the value you entered into the box.
It's looking in columns "A" to "F" of each row.

It if it did not copy any data to sheet named "New" and did not highlight any rows then it did not find that value in any cell.

Now if you enter "George" and the cell has "George went to the store" then it does not look at that as a find.

Tell me if I'm not understanding you properly.
 
Upvote 0
Hi! I searched for a term that I know is in the table and it did not copy any data to sheet "New" and did not highlight any rows in my table.

Should I remove my conditional formatting first? My conditional formatting highlights all rows in yellow that contain the search term in any cell. My search term is entered into cell B1. I apologize if I was not clear.
 
Upvote 0
Oh I see! So I just re-read your post! It will NOT consider "George went to the store" as a find if I am searching for "George"! That is the problem!

With my conditional formatting I can search for "geo" and "George went to the store" would be a find and that row would be highlighted in yellow.

So, can we modify the script to find my yellow rows and move them to the New sheet?

Thank you so very much!
 
Upvote 0
This should allow for "geo" in a mixed text string.
However, this could cause issues, if you were looking for George and Geography was in the cell. I'd suggest keeping your search as accurate as possible

Code:
Sub Check_Rows()
Application.ScreenUpdating = False
Dim i As Long, Lastrow As Long, Lastrowa As Long
Dim MyValue As String, c As Range
MyValue = InputBox("Enter value to search for")
Lastrow = Sheets("MyData").Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = Sheets("New").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("MyData").Activate
    For i = 1 To Lastrow
        For Each c In Range("A" & i & ":F" & i)
            If InStr(1, c.Value, MyValue, 1) Then
                Rows(i).Copy Destination:=Sheets("New").Rows(Lastrowa)
                Rows(i).Cells.Interior.ColorIndex = 6
                Lastrowa = Lastrowa + 1
            End If
        Next
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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