A quadratic interpolation macro

advertisement
Seventh handout
In the New Data folder in the shared drive, find and open the workbook, Quadratic
Interpolation Macro. Let us put a play button for that macro on that sheet, a way of
playing it more quickly.
1. View > Toolbars > Forms
2. Click the small button icon.
3. Move the cursor away from the button toward the worksheet. Notice the + sign
follows with the mouse. When you click again, you will place the upper left
corner of the button on the worksheet.
4. When you click, a dialog box appears and editing arrow choices are given.
5. First, choose the macro that this button will play, something like QuadPreds.
6. Then size the button the way you want with the editing arrows.
7. Click anywhere away from the button to end the button creation process.
Now click the button you just created. As promised, it begins to run a macro. The first
thing it asks for is an integer between 1 and 9, and also makes a guess of which band you
want to predict. Then it asks for the rows you want to use in the prediction. This is a
semi-automatic macro. It allows the user to run the macro differently each time. Some
may want to try different bands for the predictions and to compare results. Do a few
bands, save your results, and them look at the macro.
Now make a macro that allows the user to choose either 3 bands for quadratic
interpolation or four. Note that about half of the macro is done, the part where the user
chooses 3 bands. So your first step in creating the macro will be to ask the user whether
he wants to use 3 bands or 4 bands.
You will be doing essentially the same thing. So the first thing you may want to do is
copy, verbatim, this macro I have already created. In the editor sheet, just copy and paste
the entire macro back into the sheet. Note that Ctrl+PageDn takes you to the end of the
macro; it is easy to highlight the entire macro by Ctrl+PageUp (takes you to the
beginning), and then Shift, and then Ctrl+PageDn. You can also use the arrow keys or
the mouse. After you have made a duplicate copy then what? First, rename one of them
so that you can change that one and the old subroutine will remain in tact. I am not going
to say too much more, because I want you to think about it.
Sub RegressionStep()
'
' RegressionStep Macro
' Macro recorded 8/14/2001 by M. Lawrence Clevenson, Ph.D.
'
'
Application.Run "ATPVBAEN.XLA!Regress", ActiveSheet.Range("$A$14:$A$17"),
_
ActiveSheet.Range("$A$19:$B$22"), False, True, , ActiveSheet.Range("$Y$1") _
, True, False, False, False, , False
End Sub
Sub Band9()
Dim Col As Integer
For Col = 4 To 23
Range(Cells(9, Col), Cells(11, Col)).Copy (Cells(20, 1))
Range(Cells(1, 25), Cells(30, 40)).ClearContents
RegressionStep
Cells(23, Col) = Cells(17, 26) + Cells(18, 26) * Cells(12, Col) + Cells(19, 26) *
Cells(12, Col) ^ 2
Next
End Sub
Sub QuadPreds()
Dim Band As Integer, Row1 As Integer, Row2 As Integer, Row3 As Integer
Dim I As Integer, Answer As Integer
Band = 1
Band = InputBox("Please enter an integer from 1 to 9", "Which band to predict?",
Band)
Row1 = InputBox("Please enter the first band number you want to use (an integer
between 1 and 9)", "First band?", Band + 1)
Row2 = InputBox("Please enter the second band number you want to use (an integer
between 1 and 9)", "Second band?", Band - 1)
Row3 = InputBox("Please enter the third band number you want to use (an integer
between 1 and 9)", "Third band?", Band + 2)
Band = Band + 3 'Corresponds the band number to the row number
Row1 = Row1 + 3 'Same
Row2 = Row2 + 3 'Same
Row3 = Row3 + 3 'Same
For Col = 4 To 23
' copy the x data from the gel in col to the cells where the regression macro finds
them
Range(Cells(Row1, Col), Cells(Row1, Col)).Copy (Cells(20, 1))
Range(Cells(Row2, Col), Cells(Row2, Col)).Copy (Cells(21, 1))
Range(Cells(Row3, Col), Cells(Row3, Col)).Copy (Cells(22, 1))
' copy the y data from column 2 to the 3 values block
Range(Cells(Row1, 2), Cells(Row1, 2)).Copy (Cells(15, 1))
Range(Cells(Row2, 2), Cells(Row2, 2)).Copy (Cells(16, 1))
Range(Cells(Row3, 2), Cells(Row3, 2)).Copy (Cells(17, 1))
' clear the output area for the next regression output
Range(Cells(1, 25), Cells(30, 40)).ClearContents
RegressionStep 'do the regression
'use the coefficients from the regrssion output to predice the log molecular weight
Cells(11 + Band, Col) = Cells(17, 26) + Cells(18, 26) * Cells(Band, Col) + Cells(19,
26) * Cells(Band, Col) ^ 2
Next
Band = Band - 2 'Gives the expected default value for the next iteration
End Sub
Sub QuadPredsFourBands()
Dim Band As Integer, Row1 As Integer, Row2 As Integer, Row3 As Integer
Dim I As Integer, Answer As Integer, Row4 As Integer
Band = 1
Band = InputBox("Please enter an integer from 1 to 9", "Which band to predict?",
Band)
Row1 = InputBox("Please enter the first band number you want to use (an integer
between 1 and 9)", "First band?", Band + 1)
Row2 = InputBox("Please enter the second band number you want to use (an integer
between 1 and 9)", "Second band?", Band - 1)
Row3 = InputBox("Please enter the third band number you want to use (an integer
between 1 and 9)", "Third band?", Band + 2)
Row4 = InputBox("Please enter the fourth band number you want to use (an integer
between 1 and 9)", "Fourth band?", Band - 2)
Band = Band + 3 'Corresponds the band number to the row number
Row1 = Row1 + 3 'Same
Row2 = Row2 + 3 'Same
Row3 = Row3 + 3 'Same
Row4 = Row4 + 3 'same
For Col = 4 To 23
' copy the x data from the gel in col to the cells where the regression macro finds
them
Range(Cells(Row1, Col), Cells(Row1, Col)).Copy (Cells(20, 1))
Range(Cells(Row2, Col), Cells(Row2, Col)).Copy (Cells(21, 1))
Range(Cells(Row3, Col), Cells(Row3, Col)).Copy (Cells(22, 1))
Range(Cells(Row4, Col), Cells(Row4, Col)).Copy (Cells(23, 1))
' copy the y data from column 2 to the 3 values block
Range(Cells(Row1, 2), Cells(Row1, 2)).Copy (Cells(15, 1))
Range(Cells(Row2, 2), Cells(Row2, 2)).Copy (Cells(16, 1))
Range(Cells(Row3, 2), Cells(Row3, 2)).Copy (Cells(17, 1))
Range(Cells(Row4, 2), Cells(Row4, 2)).Copy (Cells(18, 1))
' clear the output area for the next regression output
Range(Cells(1, 25), Cells(30, 40)).ClearContents
RegressionStep2 'do the regression using 4 values of x and y
'use the coefficients from the regrssion output to predice the log molecular weight
Cells(11 + Band, Col) = Cells(17, 26) + Cells(18, 26) * Cells(Band, Col) + Cells(19,
26) * Cells(Band, Col) ^ 2
Next
Band = Band - 2 'Gives the expected default value for the next iteration
End Sub
Sub RegressionStep2()
'
Application.Run "ATPVBAEN.XLA!Regress", ActiveSheet.Range("$A$14:$A$18"), _
ActiveSheet.Range("$A$19:$B$23"), False, True, , ActiveSheet.Range("$Y$1") _
, True, False, False, False, , False
End Sub
Sub Choose3or4()
Dim Ans As Integer
Ans = InputBox("Enter either 3 to use 3 values for interpolation or 4 for 4", "Choose 3 or
4", 4)
If Ans = 3 Then
QuadPreds
Else
If Ans = 4 Then
QuadPredsFourBands
Else
MsgBox ("You need to enter either 3 or 4")
End If
End If
End Sub
Download