I have this code
Sub fillinvalues()
Dim interest As Double
Dim rate As Double
Dim payment As Double
Dim period As Double
Dim ws As Worksheet
Dim i As Object
Dim begbal As Double
Set ws = Worksheets("Sheet1")
payment = ws.Range("H3").Value
rate = ws.Range("H4").Value
begbal = ws.Range("E3").Value
begbal = 80000
Worksheets("Sheet1").Range("A2").Value = "PaymentNumber"
Worksheets("Sheet1").Range("B2").Value = "Payment/period"
Worksheets("Sheet1").Range("C2").Value = "Principal"
Worksheets("Sheet1").Range("D2").Value = "Interest"
Worksheets("Sheet1").Range("E2").Value = "RemainingBal"
ws.Range("A1:G1").Value = "Monthly Payments at effective monthy interest rate for 25-years"
ws.Range("A3").Value = 0
ws.Range("A3").Select
ws.Range("A3").Activate
period = 0
Do Until period = 301
' period = 0
ActiveCell.Offset(period, 0) = period
ActiveCell.Offset(period, 1) = payment
period = period + 1
Loop
Cells(3, 2).ClearContents
Cells(4, 4).Select
Do Until ws.Columns(1) = Empty
ActiveCell.Value = ActiveCell.Offset(-1, 1).Value * rate
ActiveCell.Offset(0, -1).Value = ActiveCell.Offset(0, -2).Value - ActiveCell.Value
'For Each i In ws.Columns(4)
'i = ActiveCell.Offset(-1, 1).Value * rate
'ActiveCell.Value = i
ActiveCell.Offset(0, 1).Value = ActiveCell.Offset(-1, 1).Value - ActiveCell.Offset(0, -1).Value
ActiveCell.Offset(1, 0).Activate
'Next i
Loop
End Sub
But I am getting the type mismatch error on cell D4 (where the 2nd loop begins after Cells(4,4). I don't understand why as I have declared rate as a double and the current cell's value is a double (see image attached). Thanks.
ws.Columns(1) = Empty
should beActiveCell = vbNullString
but you should avoid using activecell... This is super slow Try usingws.Cells(4,i)
withFor i = 4 to LastRow
and see this for lastrow : stackoverflow.com/documentation/excel-vba/918/… - R3uK