I'm trying to vlookup values in 3 worksheets in workbook wbPI against a table in second workbook wbPA. I am then replacing the #N/As and applying formatting to the positive look up results. Can anyone see why the code isn't working after the second vlookup please?
Code:
Sub LookUp_View()
Dim PAccept1 As Long, PInterv1 As Long, PInterv2 As Long, PInterv3 As Long
Dim wbPA As Workbook, wbPI As Workbook
Dim wsPA1 As Worksheet, wsPI1 As Worksheet, wsPI2 As Worksheet, wsPI3 As Worksheet
Dim PARang As Range
Set wbPA = Workbooks("Book 2")
Set wsPA1 = wbPA.Sheets(1)
Set wbPI = Workbooks("Book 1")
Set wsPI1 = wbPI.Sheets(1)
Set wsPI2 = wbPI.Sheets(2)
Set wsPI3 = wbPI.Sheets(3)
PInterv1 = wsPI1.Range("A" & Rows.Count).End(xlUp).Row
PInterv2 = wsPI2.Range("A" & Rows.Count).End(xlUp).Row
PInterv3 = wsPI3.Range("A" & Rows.Count).End(xlUp).Row
PAccept1 = wsPA1.Range("D" & Rows.Count).End(xlUp).Row
Set PARang = wsPA1.Columns("G:H")
wbPA.Activate
wsPA1.Range("H:H").Insert
Range("H1:H" & PAccept1).Value = "YES"
Range("G:H").NumberFormat = "General"
Cells.Select
Range(Cells.Address).Sort Key1:=Range("G1"), Order1:=xlAscending, Header:= _
xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
wbPI.Activate
wsPI1.Range("B:C").NumberFormat = "General"
wsPI2.Range("B:C").NumberFormat = "General"
wsPI3.Range("B:C").NumberFormat = "General"
wsPI1.Range("C2:C" & PInterv1) = Application.VLookup(wsPI1.Range("B2:B" & PInterv1), PARang, 2, False)
With wsPI1.Range("C2:C" & PInterv1)
.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Borders.LineStyle = xlContinuous
End With
wsPI1.Range("C2:C" & PInterv1).SpecialCells(xlCellTypeConstants, 23).Select
With Selection
.Interior.ColorIndex = 3
.Font.Bold = True
.Font.ColorIndex = 1
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
wsPI2.Range("C2:C" & PInterv2) = Application.VLookup(wsPI2.Range("B2:B" & PInterv2), PARang, 2, False)
With wsPI2.Range("C2:C" & PInterv2)
.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Borders.LineStyle = xlContinuous
End With
' problem here!!
' debugger highlights next row:
wsPI2.Range("C2:C" & PInterv2).SpecialCells(xlCellTypeConstants, 23).Select
With Selection
.Interior.ColorIndex = 45
.Font.Bold = True
.Font.ColorIndex = 1
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
wsPI3.Range("C2:C" & PInterv3) = Application.VLookup(wsPI3.Range("B2:B" & PInterv3), PARang, 2, False)
With wsPI3.Range("C2:C" & PInterv3)
.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Borders.LineStyle = xlContinuous
End With
wsPI3.Range("C2:C" & PInterv3).SpecialCells(xlCellTypeConstants, 23).Select
With Selection
.Interior.ColorIndex = 10
.Font.Bold = True
.Font.ColorIndex = 1
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End Sub