I am trying to create a VBA code to find the derivative of a simple polynomial. I organized the code in a way that I have to input the coefficient at the first ("A") row of the excel sheet followed by the power at the second ("B") row of the excel sheet over a range of 12 columns. The user will then manually write 12 domains from row 3 to row 14 for which he wants to find the respective derivatives. I will just need to start the subroutine and the derivatives would be found from row 3 to row 14, at the cell beside their respective domains. The following is my code:
Sub derivativecalculator()
'Declare variables
Dim domain As Double
Dim coefficient As Double
Dim derivative As Double
Dim power As Double
'Associating cells with letters to use asterisk sign in the for next loop
Cells(1, 1).Value = AA1
Cells(1, 2).Value = BB1
Cells(1, 3).Value = CC1
Cells(1, 4).Value = DD1
Cells(1, 5).Value = EE1
Cells(1, 6).Value = FF1
Cells(1, 7).Value = GG1
Cells(1, 8).Value = HH1
Cells(1, 9).Value = II1
Cells(1, 10).Value = JJ1
Cells(1, 11).Value = KK1
Cells(1, 12).Value = LL1
Cells(2, 1).Value = AA2
Cells(2, 2).Value = BB2
Cells(2, 3).Value = CC2
Cells(2, 4).Value = DD2
Cells(2, 5).Value = EE2
Cells(2, 6).Value = FF2
Cells(2, 7).Value = GG2
Cells(2, 8).Value = HH2
Cells(2, 9).Value = II2
Cells(2, 10).Value = JJ2
Cells(2, 11).Value = KK2
Cells(2, 12).Value = LL2
'Using for-next loop to determine derivatives across a range of values using general derivative formula up to power 12
For currow = 3 To 14
Cells(currow, 1).Value = domain
derivative = AA1 * AA2 * domain ^ (AA2 - 1) + BB1 * BB2 * domain ^ (BB2 - 1) + CC1 * CC2 * domain ^ (CC2 - 1) + DD1 * DD2 * domain ^ (DD2 - 1) + EE1 * EE2 * domain ^ (EE2 - 1) + FF1 * FF2 * domain ^ (FF2 - 1) + GG1 * GG2 * domain ^ (GG2 - 1) + HH1 * HH2 * domain ^ (HH2 - 1) + II1 * II2 * domain ^ (II2 - 1) + JJ1 * JJ2 * domain ^ (JJ2 - 1) + KK1 * KK2 * domain ^ (KK2 - 1) + LL1 * LL2 * domain ^ (LL2 - 1)
derivative = Cells(currow, 2).Value
Next arrow
End Sub
As shown in the above code, I declared my variables, and associated the values in the first and second row over a range of 12 columns with variables. I than substituted those variables in the derivative general formula and used a for-next loop to find the corresponding derivative for each and every domain before getting my code to display the result at the cell next to the respective domain. I have tried to run the subroutine, but excel deems there to be a logical flaw in my code. I am unable to find any errors in my code, do you see any logical errors present that I have missed?