如果循环单元格是=到上一个循环,是否有[VBA]可以为每个循环检入?

时间:2019-10-02 10:23:49

标签: excel vba range cell

我想要的是查看循环后增加的cell.value是否等于其自身,但等于先例。

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

1 个答案:

答案 0 :(得分:2)

您在这里:

    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