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 = ""
 Else
 Range("d2").Value = Range("a2").Value - Range("b2").Value
 Range("c2").Value = ""

End If

End Sub

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 = ""
        Else
            Range("D" & i).Value = Range("A" & i).Value - Range("B" & i).Value
            Range("C" & i).Value = ""
        End If
    Else
        Exit Sub
    End If
Next
End Sub

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

Shailaja M :)

Thank you very much Manoshailu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.