• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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)
AD: 36
AE: Will be manually typed
AF: =AE7/12
AG: =PMT(AF7,AD7,-AC7,Z7)

 Excel
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!
0
varesources
Asked:
varesources
  • 4
  • 3
  • 2
1 Solution
 
Saqib Husain, SyedEngineerCommented:
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
 
varesourcesAuthor 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
 
EmenizerCommented:
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 & ")"
        '... add other formulas here
    End If
Next rngCell

End Sub

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
varesourcesAuthor Commented:
The range of Y is always different so is there a way to make it relative to the document?
0
 
Saqib Husain, SyedEngineerCommented:
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
 
EmenizerCommented:
@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
 
varesourcesAuthor 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
 
varesourcesAuthor Commented:
I set the range to a number of rows that will never be on this worksheet. Thanks!
0
 
Saqib Husain, SyedEngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now