Search code examples
excelexcel-formulaexcel-2010excel-2007vba

How to highlight the cell in excel if its blank on comparison in different sheets?


I am trying to highlight the cell in sheet1 which are blank on comparing it with sheet 2.

I tried to this with conditional formating but I am not able to get the logic to do it . Please check the sheet . I want the cell being highlight in sheet 1 if its blank on comparing the sheet 2. This task could be easy for you but not for me.

For example . In sheet 2 , row 5 is empty then, it should highlight it in sheet 1.

Excel sheet

Logic Sheet1(Latest)    Sheet2(old) 

      Blank             Blank    No highlight 
      Blank             Date     Highlight in sheet 1
      Date              Date     No highlight in sheet 1
      Date              Blank    Highlight in sheet 1

Pls help


Solution

  • The following routine will take care of your present problem:

    Dim i As Long
    Dim lastrow1 As Long, lastrow2 As Long, biggerlastrow As Long
    
    lastrow1 = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
    lastrow2 = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
    
    If lastrow1 > lastrow2 Then biggerlastrow = lastrow1
    If lastrow1 < lastrow2 Then biggerlastrow = lastrow2
    If lastrow1 = lastrow2 Then biggerlastrow = lastrow2
    
    
    For i = 1 To biggerlastrow
         If Sheet2.Cells(i, 1) = "" Then
            Sheet1.Cells(i, 1).Select
            ActiveCell.Interior.ColorIndex = 6
         End If
    
    Next