Problem with formatting VLOOKUP results across multiple tabs

spidaman

Board Regular
Joined
Jul 26, 2015
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Can anyone offer a fix please for a VLOOKUP problem I am struggling to resolve? :confused:

I am using a look-up table in "Book 2" to add the text value "YES" to columns on three different Sheets in "Book 1". I am then replacing the "#N/A" with "". Then using:

Code:
.SpecialCells(xlCellTypeConstants, 23).Select

...to select the cells left in the range with "YES" values. I then want to apply formatting to the "YES" cells in each of the sheets. I am currently getting an error with the second instance of

Code:
.SpecialCells(xlCellTypeConstants, 23).Select

...where I am trying to select the "YES" value cells on Sheet 2 of wbPI Workbook.

Why does the
Code:
.SpecialCells(xlCellTypeConstants, 23)
work for Sheet 1 but not for Sheet 2?
Can anyone identify the problem please or suggest a better way of removing the "#N/A" and selecting the remaining "YES" value cells?

This is the full code:

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


                        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

Thanks very much in advance
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

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