Tariro Manyika
Tariro Manyika

Reputation: 1

adding and multiplying loop in vba

I want to take variable A add variable B then multiply the result by variable C

D= (A + B)*C

then take result D add variable B then multiply the result by variable C

basically I want to add B then multiply by C the result on a loop then stop after n times

this is what I have tried so far:

Dim A As Variable

Dim C As Integer

Dim B As Integer

Dim Answer As Integer

C = 3

B = 5

Answer = (A + B)*C

Worksheets(1).Range("B3").Value = Answer

I'm basically trying to loop this process n times

Upvotes: 0

Views: 443

Answers (1)

cc585
cc585

Reputation: 69

If you want to do a loop, you will need a For Next statement, Do While statement, Do until statement or For each statement. And you might need to change the variable types from integer to long depending on how large the calculation is.

Sub calculation()

Dim i As Integer, n As Integer
Dim a As Long, b As Long, c As Long

a = 1
b = 0
c = 10

n = InputBox("Loop n times")

If n = 0 Then
    a = 0
    MsgBox (a)
Else
    For i = 1 To n
    a = (a + b) * c
    Next
    MsgBox (a)
End If

End Sub


Upvotes: 1

Related Questions