Home » excel » excel – Is there a way [VBA] to check in a for each loop if the looping cell is = to the previous?

excel – Is there a way [VBA] to check in a for each loop if the looping cell is = to the previous?

Posted by: admin May 14, 2020 Leave a comment

Questions:

What i want is to look if the cell.value that increments, after the loop, is equals to itself but of precedent cicle.

If Not Intersect(Target, rng) Is Nothing Then
  risorsa = Cells(Target.ROW, 3).Value
  For Each cell In rngCalendario 
    If UCase(cell.Value) Like "*" & risorsa & "*" Then '
      For i = cell.ROW To RigaNomeProgetto Step -1 
        If Cells(i, 2).Value = 0 And cell.ROW < ultimaRiga Then 
          NomeProgetto = Cells(i, 1).Value 
          Exit For
        End If
      Next i
      Output = LTrim(Output) & NomeProgetto & Chr(10) & Chr(10) & Chr(10)  
    End If
  Next cell
  Cells(2, 4) = Output
End if
How to&Answers:

Here you go:

    If Not Intersect(Target, rng) Is Nothing Then

    Dim prevCell As Variant 'Choose applicable type, probably String
    risorsa = Cells(Target.Row, 3).Value

    For Each cell In rngCalendario
      If UCase(cell.Value) Like "*" & risorsa & "*" And prevCell = cell.Value Then 'current cell value is compared to previous cell
        For i = cell.Row To RigaNomeProgetto Step -1
          If Cells(i, 2).Value = 0 And cell.Row < ultimaRiga Then
            NomeProgetto = Cells(i, 1).Value
            Exit For
          End If
        Next i
        Output = LTrim(Output) & NomeProgetto & Chr(10) & Chr(10) & Chr(10)
      End If
      prevCell = cell.Value 'prevCell is assigned the value of the current cell
    Next cell
    Cells(2, 4) = Output
End If