title: Conditional Statement Not Working Properly in VBA Excel
body: <p>I am writing a code in VBA that loops through every row for several columns and finds the difference between every two rows and stores that value in an array. I added an if statement in the for loop to avoid blank cells being included in the operation, and it works for the first five columns but not for the sixth. </p>
  
  <pre><code>Dim Datarray As Variant
  Dim LR As Long, LR1 As Long
  Dim p As Integer, i As Integer
  LR1=LR-1
  ReDim R(LR1) As Variant
  
  For i = 1 To DateTime_Column
  
  LR = Cells(Rows.Count, i).End(xlUp).Row
  Datarray = Range(Cells(1, i), Cells(LR, i))
  
   For p = 2 To LR1
  
     If Datarray(p, 1) And Datarray(p + 1, 1) &lt;&gt; Empty Then  
      R(p) = Abs(Datarray(p + 1, 1) - Datarray(p, 1))
     End If
  
   Next p
  
  Rave = WorksheetFunction.Average(R)       ***Error occurs here because R is empty***
  
  Next i
  </code></pre>
  
  <p>Does anyone see anything wrong with the way my if statement is written? The code seems to think that the cells in the sixth column are empty when they are not. I checked the Datarray and every spot has values in it.  </p>