PDA

View Full Version : [SOLVED:] Need some very basic help



adilh301
06-13-2016, 06:55 AM
I'm new to VBA and just want to understand some of my code.

the book im working from does very little to explain this but what am i doing here? I know when complete it populates a table but how is this section of code doing this?




'loop to calculate and output year-by year amort. table
For rowNum = 1 To LoanLife


intComp = yrBegBal * intrate
princerepay = annualpmnt - intComp
YrEndBal = yrBegBal - princerepay


Cells(outRow + rowNum + 3, 3).Value = rowNum
Cells(outRow + rowNum + 3, 4).Value = yrBegBal
Cells(outRow + rowNum + 3, 5).Value = annualpmnt
Cells(outRow + rowNum + 3, 6).Value = intComp
Cells(outRow + rowNum + 3, 7).Value = princerepay
Cells(outRow + rowNum + 3, 8).Value = YrEndBal


yrBegBal = YrEndBal


Next rowNum




some other parts of the overall program that may help you to help me.


outRow = 5 'Used to control where the output table will start
outSheet = "Loan"

mdmackillop
06-13-2016, 10:04 AM
Step through the code using F8
Use the Watch window to see how values change


'loop to calculate and output year-by year amort. table


For rowNum = 1 To LoanLife
'Get variable values from the product of other variables
intComp = yrBegBal * intrate
princerepay = annualpmnt - intComp
YrEndBal = yrBegBal - princerepay

'Output to specific cell locations defined by Cell(Row, Column)
Cells(outRow + rowNum + 3, 3).Value = rowNum
Cells(outRow + rowNum + 3, 4).Value = yrBegBal
Cells(outRow + rowNum + 3, 5).Value = annualpmnt
Cells(outRow + rowNum + 3, 6).Value = intComp
Cells(outRow + rowNum + 3, 7).Value = princerepay
Cells(outRow + rowNum + 3, 8).Value = YrEndBal

'Assign old value to new value
yrBegBal = YrEndBal

'Start loop again
Next rowNum

SamT
06-13-2016, 10:19 AM
outRow, rowNum, LoanLife, intComp, yrBegBal, intrate, princerepay, annualpmnt, YrEndBal, yrBegBal are mnemonic names of variables or Functions.

I recomend that you edit all of them to use CamelCase
OutRow, RowNum, LoanLife, IntComp, YrBegBal, IntRate, PrinceRepay, AnnualPmnt, YrEndBal, YrBegBal

For rowNum = 1 To LoanLife
In a loop, change the value of rowNum by one for each loop and stop when rowNum = LoanLife.
Next means to run the loop again

Cells is an Excel Collection that contains all the cells on a worksheet
Cells(outRow + rowNum + 3, 3).Value = rowNum
Means the Cell in the (outRow + rowNum + 3) and the third Column(C:C)
Cells(Rrow Number, Column Number or Column Letter) to return one specific Cell on a worksheet.

yrBegBal = YrEndBal
Next
Means make the value of yrBegBal = the value of YrEndBal before running the loop again

Try reading the code out loud to yourself.

adilh301
06-13-2016, 12:31 PM
Thanks for the help guys totally understand it now was the outrow thing that confused me.