# How do I male a macro that does the following calculations?

In the image below the yellow cell is a number that I am trying to get a monthly payment for. Here are the calculations that I need in the following cells.

Z: =Y7*AA7
AA: Will be manually typed
AB: Will be manually typed
AC: =Y7*(1+AB7)
AE: Will be manually typed
AF: =AE7/12

I need these calculations to only show up to the right of the yellow cells (or Y cells that have a number in them but the X just to the left is blank).

I hope this isn't too confusing. Thanks!
LVL 1
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EngineerCommented:
use this formula for each of the above formulas

=if(and(x7="",y7<>""),formula,"")

and copy down.

You should leave the "Will be manually typed" cells blank before the copy.

Saqib
0
Author Commented:
I need it to be in a macro if at possible. I am trying to make this as non manual as possible. There are thousands of rows and this has to be done every day.
0
Commented:
Try this code
You should of course reset the range of Y column
``````Sub fillFormulas()

Dim rngRange As Range
Dim rngCell As Range
Dim lngRow As Long

'define range to set formulas
Set rngRange = Worksheets("Sheet1").Range("Y7:Y12")

'loop through cells of range and set formulas where applicable
For Each rngCell In rngRange
If rngCell.Value <> Empty And rngCell.Offset(0, -1).Value = Empty Then
lngRow = rngCell.Row
rngCell.Offset(0, 1).Formula = "=Y" & lngRow & "*AA" & lngRow
rngCell.Offset(0, 4).Formula = "=Y" & lngRow & "*(1+AB" & lngRow & ")"
End If
Next rngCell

End Sub
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
The range of Y is always different so is there a way to make it relative to the document?
0
EngineerCommented:
If it is just this one file then you do not need a macro.

Just copy the row all the way down and it will take care of which row to be displayed.

just remember to clear the "manual entry" cells before copying to make life easier.

Saqib
0
Commented:
@varesources: what do you mean with 'relative to the file'?
Is it a fixed range for one particular file? Or can it be variable in one file?
0
Author Commented:
The data will be copied in everyday to this workbook that has the macros. The amount of data changes everyday so the number of rows could vary by hundreds of rows.
0
Author Commented:
I set the range to a number of rows that will never be on this worksheet. Thanks!
0
EngineerCommented:
I was just about to post my version of the code when you accepted the answer. Anyway here it is

Sub populateformulas()
frmla = Split(String(26, ";") & "=Y#*AA#;;;=Y#*(1+AB#);36;;=AE#/12;=PMT(AF#,AD#,-AC#,Z#)", ";")
For Each rw In ActiveSheet.UsedRange.Rows.EntireRow
If rw.Cells(1, 24) = "" And rw.Cells(1, 25) <> "" Then
For i = 26 To 33
rw.Cells(1, i).Formula = Replace(frmla(i), "#", rw.Row)
Next i
End If
Next rw
End Sub
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.