hi all
i am newbie in Excel VBA
i created list in Excel and i wrote into (a1 cell-income, b1 cell-expence, c1 cell-kredit and d1 cell-debet)
i enter a2 cell income (for ex:1000) and b2 cell expence (for ex: 100)
and I give condition in VBA that if income greater than expence then VBA writes a2.value-b2.value into d2 cell
else if expence greater than income then it writes b2.value-a2.value into c2 cell
but i only define this one row (for ex: c2, a2. b2. d2)
i would like to assign it the rows as long as i want
that is i would like to create loop
(for ex: a(i), b(i) and so on

here is code

pls help me i very need this code
thanks beforehands

Sub initialize()
Dim i As Integer
Dim lastrow As Long

If (Range("b2").Value > Range("a2").Value) Then
 Range("c2").Value = Range("b2").Value - Range("a2").Value
 Range("d2").Value = ""
 Range("d2").Value = Range("a2").Value - Range("b2").Value
 Range("c2").Value = ""

End If

End Sub

Edited 7 Years Ago by azegurb: n/a

Check the below code.

Sub calculate()
Dim i As Long

For i = 2 To 1048576   'No. of rows in the Ms Excel 2007. U can modify according to ur excel version
    If Range("A" & i).Value <> "" Then
        If Range("A" & i).Value > Range("B" & i).Value Then
            Range("C" & i).Value = Range("B" & i).Value - Range("A" & i).Value
            Range("D" & i).Value = ""
            Range("D" & i).Value = Range("A" & i).Value - Range("B" & i).Value
            Range("C" & i).Value = ""
        End If
        Exit Sub
    End If
End Sub

I think this may help you for your requirements. Enjoy!!!

Shailaja M :)

This question has already been answered. Start a new discussion instead.