UsedRange in all worksheets Wb1 to find matching value in Wb2.

BRB1983

Board Regular
Joined
Aug 29, 2019
Messages
61
I'm trying to get workbook 1, all sheets used range, if cell value = a cell value in column a of sheets"Final" in Wb2. if so color cell indexcolor 6. the following cod runs but no color change.
Code:
Sub color_matching_between_workbooks()
    Dim sh As Worksheet
    'Dim wb1 As Workbook
    Dim wb2 As Workbook
    Set wb2 = Workbooks.Open("C:\Users\Desktop\17057 System Line List.xlsm")
    Dim finalrow As Integer
    Dim i As Integer
    finalrow = wb2.Sheets("Final").Range("A6000").End(xlUp).Row
    For i = 2 To finalrow
    
    
    For Each sh In ThisWorkbook.Worksheets
Dim cell As Range


For Each cell In sh.usedrange
If cell.Value = i Then
cell.Interior.ColorIndex = 4
End If


Next cell
Next sh
    
    Next i
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try:
Code:
Sub color_matching_between_workbooks()
    Application.ScreenUpdating = False
    Dim sh As Worksheet, srcWS As Worksheet, wb1 As Workbook, wb2 As Workbook, rng As Range, fnd As Range
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks.Open("C:\Users\Desktop\17057 System Line List.xlsm")
    Set srcWS = Sheets("Final")
    For Each sh In wb1.Sheets
        For Each rng In sh.UsedRange
            Set fnd = srcWS.Range("A:A").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                rng.Interior.ColorIndex = 6
            End If
        Next rng
    Next sh
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mumps, ran into small issue, notice that it colors some random ranges. i double check that the cells are clear. i can not figure it out.
 
Upvote 0
Annotation%202019-08-29%20180408.png
 
Upvote 0
Try this version of the macro. It will clear any existing color fill before starting the search each time you run the macro.
Code:
Sub color_matching_between_workbooks()
    Application.ScreenUpdating = False
    Dim sh As Worksheet, srcWS As Worksheet, wb1 As Workbook, wb2 As Workbook, rng As Range, fnd As Range
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks.Open("C:\Users\Desktop\17057 System Line List.xlsm")
    Set srcWS = Sheets("Final")
    For Each sh In wb1.Sheets
        sh.UsedRange.Cells.Interior.ColorIndex = xlNone
        For Each rng In sh.UsedRange
            Set fnd = srcWS.Range("A:A").Find(rng, LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                rng.Interior.ColorIndex = 6
            End If
        Next rng
    Next sh
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
still the same issue. So this may help. i have 30 tabs that this code look at. so on the first tab is where it is coloring in unused cells. ("A1:N54") the is nothing in these cells. i cleared the cells to be sure. and the funny part is the longer the code runs, the wider the range gets. color sets in at end of code.
 
Upvote 0
Are you saying that the problem is that the macro is coloring blank cells otherwise it works properly?
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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