title: VBA If-ElseIf statements do not return correct results
body: <p>I'm having trouble with my If-ElseIf statements since the cell values did not return correct results.</p>
  
  <p>Below are the codes in my Module:</p>
  
  <p>1st batch:</p>
  
  <pre><code>Sub compare2()
  Dim i As Long
  Dim A As Long
  Dim B As Long
  Dim c As Long
  
  A = 14
  B = 15
  c = 16
  
  Do While A &lt;= 42
  i = 2
  Do Until Len(Cells(i, A)) = 0
  
  If Cells(i, A) = "Green" And Cells(i, B) = "Rollup" Then
  Cells(i, c) = "Green"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Rollup" Then
  Cells(i, c) = "Rollup"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Green" Then
  Cells(i, c) = "Green"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Yellow" Then
  Cells(i, c) = "Yellow"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Red" Then
  Cells(i, c) = "Red"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Overdue" Then
  Cells(i, c) = "Overdue"
  
  ElseIf Cells(i, A) = " " And Cells(i, B) = " " Then
  Cells(i, c) = " "
  
  Else
  
  End If
  
  i = i + 1
  
  Loop
  
  
  A = A + 4
  
  B = A + 1
  
  c = A + 2
  
  Loop
  
  
  End Sub
  </code></pre>
  
  <p>2nd batch in Module:</p>
  
  <pre><code>Public Sub single_change(changed_cell As Range)
  
      Dim sales_cell As Range
      Dim production_cell As Range
      Dim day_cell As Range
      Dim col_num As Integer
  
  col_num = changed_cell.Column
  If changed_cell.Column &lt; 14 Then 'Dont do anything before Col N
  Exit Sub
  Else
  col_num = changed_cell.Column - 14
  End If
  
  If col_num Mod 4 = 0 Then
  
  Set sales_cell = changed_cell
  Set production_cell = changed_cell.Offset(, 1)
  Set day_cell = production_cell.Offset(, 1)
  
  ElseIf (col_num - 1) Mod 4 = 0 Then
  
  Set sales_cell = changed_cell.Offset(, -1)
  Set production_cell = changed_cell
  Set day_cell = production_cell.Offset(, -1)
  
  Else
  
  'Dont do anything between Col N,O and their repeated values
  
  Exit Sub
  
  End If
  
  On Error GoTo multiple_changes
  
  If sales_cell = "Green" And production_cell = "Rollup" Then
  day_cell = "Green"
  
  ElseIf sales_cell = "Rollup" And production_cell = "Rollup" Then
  day_cell = "Rollup"
  
  ElseIf sales_cell = "Rollup" And production_cell = "Green" Then
  day_cell = "Green"
  
  ElseIf sales_cell = "Rollup" And production_cell = "Yellow" Then
  day_cell = "Yellow"
  
  ElseIf sales_cell = "Rollup" And production_cell = "Red" Then
  day_cell = "Red"
  
  ElseIf sales_cell = "Rollup" And production_cell = "Overdue" Then
  day_cell = "Overdue"
  
  ElseIf sales_cell = " " And production_cell = " " Then
  day_cell = " "
  
  Else
  'Do nothing
  End If
  Exit Sub
  
  multiple_changes:
  Dim i As Long
  Dim A As Long
  Dim B As Long
  Dim c As Long
  
  A = 14
  B = 15
  c = 16
  
  Do While A &lt;= 42
  i = 2
  Do Until Len(Cells(i, A)) = 0
  
  If Cells(i, A) = "Green" And Cells(i, B) = "Rollup" Then
  Cells(i, c) = "Green"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Rollup" Then
  Cells(i, c) = "Rollup"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Green" Then
  Cells(i, c) = "Green"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Yellow" Then
  Cells(i, c) = "Yellow"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Red" Then
  Cells(i, c) = "Red"
  
  ElseIf Cells(i, A) = "Rollup" And Cells(i, B) = "Overdue" Then
  Cells(i, c) = "Overdue"
  
  ElseIf Cells(i, A) = " " And Cells(i, B) = " " Then
  Cells(i, c) = " "
  
  Else
  End If
  i = i + 1
  Loop
  
  A = A + 4
  B = A + 1
  c = A + 2
  
  Loop
  End Sub
  </code></pre>
  
  <p>And this is in the worksheet:</p>
  
  <pre><code>Private Sub Worksheet_Change(ByVal Target As Range)
  
      Call Module1.single_change(Target)
  
  End Sub
  </code></pre>
  
  <p>Originally my codes had Runtime Error "Application Defined or Object-defined Error" but all <code>If</code> and <code>ElseIf</code> statements returned correct values. After changing my codes to the one a user recommended (see here <a href="https://stackoverflow.com/questions/59970534/vba-runtime-error-1004-application-defined-or-object-defined-error-when-using">VBA Runtime Error 1004 “Application-defined or Object-defined error” when using Range.Offset</a>), cell value always change on its own accord even though I put a different value.</p>
  
  <p>For example, when I entered <code>Rollup</code> in cell N2 (column Sales) then <code>Green</code> in cell O2 (column Production), cell P2 (column Day) was supposed to return <code>Green</code> per the <code>ElseIf sales_cell = "Rollup" And production_cell = "Green" Then day_cell = "Green"</code>. However, when I pressed Enter after filling cell O2 (Production), cell N2 (Sales) automatically changed to <code>Green</code>. Then after that, I changed cell N2 back to <code>Rollup</code> and the Macro worked.</p>
  
  <p><a href="https://i.stack.imgur.com/H5h6g.png" rel="nofollow noreferrer">Print screen from result</a></p>
  
  <p>Does anyone know what's going on? Thank you very much! Any help is much appreciated!!! </p>