|
Third-Party Research
& Development
Technical Discourse A VBA Program For Least Squares Polynomial Fits: Using Excel Linest Function John R. Potts, Senior Scientist/Engineering Manager BRIEF Microsoft's Visual Basic for Applications (VBA) is not quite as straightforward and easy to use as Microsoft would have you believe. The Visual Basic Editor is provided free with Microsoft Office tools and accessible with the keystroke ALT+F11. Learning the basics of VBA doesn't take too long, especially if you selectively read an excellent reference like Excel 2002 Power Programming with VBA by John Walkenbach (ISBN: 0-7645-4799-2). Problems arise when you try more advanced things like making an Excel Add-in, which is a program attached to Excel to get more functionality, or try to run some of the more useful and powerful Excel Functions like LINEST. This paper shows explicit VBA code, a program called Polyfit2, which I developed for my projects, that does Least Squares Polynomial Fits for Excel Data, using the Excel LINEST function. Feel free to use the code as you see fit (no pun intended)! I don't assume any liability for its use (read the disclaimer attached). I think VBA and VB are well worthwhile learning, however, feel obligated to mention a serious shortcoming: a general lack of code security for VBA. Microsoft needs to rethink and revise its approach to VBA code security because the single password protection offered as a means of protecting VBA code with Add-Ins is absolutely NO PROTECTION AT ALL. Make up any 32 mixed character password and it will be broken in less than a second with a $45 commercial "password recovery tool". For reasonable VBA code protection, one has to spend at least $500 and a considerable amount of time learning how to make compiled VBA COM Add-ins. Figure 1 shows some data, generated with the polynomial formula shown on the spreadsheet, which was used to test Polyfit2. Notice that the coefficients from the least squares polynomial fit closely agree with those of the polynomial used to generate the data..
Figure 1. Results of Running Polyfit2. Please feel free to email me any questions or comments that you may have regarding program operation or design. THE PROGRAM Sub polyfit2 (Startcell As String, DPM1 As Integer,
OM1 As Integer) ReDim b(0 To regorder) LinestOut = Application.LinEst(ally, allxeq, True, True) For i = 0 To regorder R2 = LinestOut(3, 1) Debug.Print "R2", R2 End Sub
|