/ / Si el rango de condición no funciona - excel, vba, excel-vba

Si el rango de condición no funciona - excel, vba, excel-vba

No puedo hacer que el código de abajo funcione cuandocopia y pega valores en las columnas A, C y D. Si, por ejemplo, estoy en la fila 2 e inserto datos en las celdas A2, C2 y D2, el código hace su trabajo. Pero si copio y pego datos en las filas 3, 4, 5, el código ya no verificará la condición. Realmente apreciaría si alguien me puede ayudar a solucionar este problema. ¡Gracias!

Sub LogicalPart(i As Long)

If (Cells(i, "C") + Cells(i, "D")) <> Cells(i, "A") Then
MsgBox "C" & i & " + D" & i & " must equal cell A" & i & " which is: " & Range("A" & i).Value
MsgBox ("Please insert again the data in cell C" & i & " or D " & i & "!")
Cells(i, "C").Interior.Color = RGB(255, 0, 0)
Cells(i, "D").Interior.Color = RGB(255, 0, 0)

Else
Range(Cells(i, "C"), Cells(i, "D")).Interior.Color = RGB(1000, 1000, 1000)
End If

End Sub



Private Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Range("C1:d10"), Target) Is Nothing Then
LogicalPart Target.row
End If


End Sub

Respuestas

3 para la respuesta № 1

Necesitas iterar las filas en el rango objetivo:

Target.Row

Solo devolverá la primera fila y no todas ellas:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim rngrow As Range
Set rng = Intersect(Range("C1:d10"), Target)
If Not rng Is Nothing Then
For Each rngrow In rng.Rows
LogicalPart rngrow.Row
Next rngrow
End If


End Sub