View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
john john is offline
external usenet poster
 
Posts: 97
Default if a=true and b=5 then....

I added two more features....John

Option Explicit
Sub Macro1()
'
' Macro1 Macro
'
Sheets("payment_schedule").Select
Dim lastrow As Integer
Dim firstrow As Integer
Dim month(50) As Date
Dim startmonth(100) As Date
Dim Noofpayments(100) As Integer
Dim TotalPayment(100) As Double
Dim paymentsmade(100) As Double
Dim monthtotal(50) As Double
Dim totalmonths As Integer
Dim i As Integer
Dim j As Integer
'
'Allow up to 300 rows of data in Column A
Cells(1, 1).Offset.End(xlDown).Select
firstrow = ActiveCell.Row
Cells(300 + firstrow, 1).Offset.End(xlUp).Select
lastrow = ActiveCell.Row
'
Cells(firstrow, 1).Offset.End(xlToRight).Select
totalmonths = ActiveCell.Column - 4
'
' Read in Data
' Sheet set up as follows:
' 1st Payment date is in column A, Name in column B, Total
Payment in Column C
' # of Payments in Column D, and the months of payments
due (June, July, etc)
' start in Column E
'
' 1st payment Dates
For i = firstrow + 1 To lastrow
startmonth(i) = Cells(i, 1).Value
Next i
'
' Total Payments
For i = firstrow + 1 To lastrow
TotalPayment(i) = Cells(i, 3).Value
Next i
' # of Payments
For i = firstrow + 1 To lastrow
Noofpayments(i) = Cells(i, 4).Value
Next i
' Payment Months
For i = 5 To totalmonths + 4
month(i) = Cells(firstrow, i).Value
Next i
For i = firstrow + 1 To lastrow
paymentsmade(i) = 0
For j = 5 To totalmonths + 4
If month(j) < startmonth(i) Then GoTo nextj
If paymentsmade(i) 0 Then GoTo notest
If Noofpayments(i) + j totalmonths + 5 Then GoTo addmonth
notest:
If paymentsmade(i) = TotalPayment(i) Then GoTo nexti
Cells(i, j).Value = TotalPayment(i) / Noofpayments(i)
'
' Format Output area
'
Cells(i, j).NumberFormat = "#,##0.00_);[Red](#,##0.00)"
Cells(i, j).HorizontalAlignment = xlCenter
paymentsmade(i) = paymentsmade(i) + _
TotalPayment(i) / Noofpayments(i)
nextj:
Next j
nexti:
Next i
'Add Monthly Totals
Cells(lastrow + 2, 1) = "Totals"
For j = 5 To totalmonths + 4
monthtotal(j) = 0
For i = firstrow + 1 To lastrow
monthtotal(j) = monthtotal(j) + Cells(i, j).Value
Next i
Cells(lastrow + 2, j).Value = monthtotal(j)
Cells(lastrow + 2, j).NumberFormat = "#,##0.00_);[Red]
(#,##0.00)"
Cells(lastrow + 2, j).HorizontalAlignment = xlCenter
Next j
GoTo theend
addmonth:
MsgBox ("You need to add another month!")
theend:
End Sub