dimanche 31 octobre 2021

Is there an easier way to write this IF statement?

I'm very new to VBA and trying to learn as I go along. I'm 100% sure there is an easier way to condense this code rather than typing IF statements for each cell and corresponding cell.

Here is the code I'm using:

        Sub Test()
If Range("B1").Value > 0 Then
    Range("A1").Value = 0
End If

If Range("B2").Value > 0 Then
    Range("A2").Value = 0
End If

If Range("B3").Value > 0 Then
    Range("A3").Value = 0
End If

If Range("B4").Value > 0 Then
    Range("A4").Value = 0
End If

If Range("B5").Value > 0 Then
    Range("A5").Value = 0
End If

If Range("B6").Value > 0 Then
    Range("A6").Value = 0
End If

End Sub

Picture

The range I'm using is A1:A6 then comparing that against values of B1:B6 and saying if B1 > 0 then A1 = 0, then repeat for A2 and B2 and so on..

I know that the solution is probably painfully obvious, but I can't seem to figure out how to do it yet.

Thanks for any help provided!

Aucun commentaire:

Enregistrer un commentaire