HELP! VBA Highlight Cells which match value

jakel27

New Member
Joined
Jun 1, 2022
Messages
39
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone,

I am very new to VBA and am trying to copy cells from one sheet to another if the cells values match. I'm doing this by pasting cells which are highlighted.

Right now I just need help with highlighting cells which match.

Scenario... If Col(E) is todays date, I want to grab all the numbers from Col(C) in the same row and search Col(C) for all other rows with the same matches and highlight them red as well.

I think my code needs another range to search within, aside from the range I'm already in.

Thanks in Advance!

1654141163500.png




Sub Highlight_Cells_and_Copy()

'Highlight
Dim R As Range, WOCell As Range, datetoday As Range
Dim WO As String, Var As String
Set R = Range("C15000", Range("C15000").End(xlDown)) 'look at this range
Set datetoday = Range("E15000", Range("E15000").End(xlDown))

Columns(3).Interior.Color = xlNone

For Each Cell In datetoday

If Cell.Value = Date Then
Cell.Offset(0, -2).Interior.Color = RGB(255, 0, 0)
Var = Cell.Offset(0, -2).Value

If InStr(WOCell.Value, Var) > 0 Then <--------------------------- FIX THIS PLEASE (Search from range R and not range datetoday
WOCell.Interior.Color = RGB(255, 0, 0)
End If

End If

Next
--------------------------------------------------------------------------- Attempt 2:
For Each WOCell In R 'look at range to only today
WO = WOCell.Value
If InStr(WOCell.Value, WO) > 0 Then
WOCell.Interior.Color = RGB(255, 0, 0)

End If

Next WOCell
 

Attachments

  • 1654141039204.png
    1654141039204.png
    14.5 KB · Views: 6

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think I got it working by combining my 2 attempts together as so... (if anyone is interested or maybe can refine my work. I wasn't sure if I could have a FOR statement within my IF statement.

Sub Highlight_Cells_and_Copy()

'Highlight
Dim R As Range, WOCell As Range, datetoday As Range
Dim WO As String, Var As String
Set R = Range("C15000", Range("C15000").End(xlDown)) 'look at this range
Set datetoday = Range("E15000", Range("E15000").End(xlDown))

Columns(3).Interior.Color = xlNone

For Each Cell In datetoday

If Cell.Value = Date Then
Cell.Offset(0, -2).Interior.Color = RGB(255, 0, 0)
Var = Cell.Offset(0, -2).Value

For Each WOCell In R
If InStr(WOCell.Value, Var) > 0 Then
WOCell.Interior.Color = RGB(255, 0, 0)
End If
Next WOCell
End If

Next
 
Upvote 0
Solution

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

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