Herbalgiraffe
New Member
- Joined
- Feb 22, 2020
- Messages
- 16
- Office Version
- 365
- Platform
- Windows
Hey Y'all,
So I am making a loop to check a list in one workbook against a list in another workbook, but when I try to run the code, it only accepts the declaration for the workbook that is active and gives a runtime 9 "object out of range" when it gets to the second workbook, and if I am on the other workbook, it will give the same error for the first workbook that I am no longer looking at. If I stick a line in to activate the other sheet, it errors out with "Can't execute code in break mode". How do I successfully declare both ranges on the two different workbooks so that I can get into the loop?
So I am making a loop to check a list in one workbook against a list in another workbook, but when I try to run the code, it only accepts the declaration for the workbook that is active and gives a runtime 9 "object out of range" when it gets to the second workbook, and if I am on the other workbook, it will give the same error for the first workbook that I am no longer looking at. If I stick a line in to activate the other sheet, it errors out with "Can't execute code in break mode". How do I successfully declare both ranges on the two different workbooks so that I can get into the loop?
VBA Code:
Sub Highlight()
Dim Ws1 As Range, Ws2 As Range, Wb1 As Workbook, Wb2 As Workbook, C As Range, fn As Range
Workbooks.Open "T:\Customer Service\Site\Pre-Order Skus.xlsx"
Set Wb1 = Workbooks("Test Order File.xlsm")
Set Ws1 = Wb1.Worksheets("Test").Range("A1", Worksheets("Test").Cells(Rows.Count, 1).End(xlUp))
Set Wb2 = Workbooks("Pre-Order Skus.xlsx")
'Here is where I tried activating the other workbook to get it to see Ws2.
Set Ws2 = Wb2.Worksheets("Sheet1").Range("A1", Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp))
Worksheets("Sheet1").Range("A1").Select
For Each C In Ws1
Set fn = Ws2.Find(C.Value, , xlValues, xlWhole)
If Not fn Is Nothing Then
C.Interior.Color = vbYellow
End If
Next
End Sub
Last edited by a moderator: