斜率或截距在VBA基本迴歸入門中有提到使用EXCEL內建函數,這邊簡單介紹一下EXCEL好用的內建函數,如何在EXCEL VBA中直接使用。
2種方式:
application
Application.WorksheetFunction
舉例1:sum函數,透過以上兩種語法結果都是一樣的。
application.sum(activesheet.range("a1:a10"))
Application.WorksheetFunction.sum(activesheet.range("a1:a10"))
但在VBA編輯時,
Application.WorksheetFunction可以自動帶出下拉式選單方便選擇。
圖1.
來選擇要回傳斜率或截距等資料,但透過VBA,可以一次取存斜率或截距等資料,
不過是透過陣列的方式。如圖2.
圖2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Private Sub CommandButton1_Click() | |
s1 = ActiveSheet.Range("A2000").End(xlUp).Row | |
B1 = Application.WorksheetFunction.LinEst(ActiveSheet.Range("c2:" & "c" & s1), ActiveSheet.Range("b2:" & "b" & s1)) | |
If B1(2) > 0 Then | |
MsgBox "y=" & Format(B1(1), "##.00") & "+" & Format(B1(2), "##.00") & "x" | |
Else | |
MsgBox "y=" & Format(B1(1), "##.00") & "-" & Format(B1(2), "##.00") & "x" | |
End If | |
MsgBox "第二次" | |
B1 = Application.LinEst(ActiveSheet.Range("c2:" & "c" & s1), ActiveSheet.Range("b2:" & "b" & s1)) | |
If B1(2) > 0 Then | |
MsgBox "y=" & Format(B1(1), "##.00") & "+" & Format(B1(2), "##.00") & "x" | |
Else | |
MsgBox "y=" & Format(B1(1), "##.00") & "-" & Format(B1(2), "##.00") & "x" | |
End If | |
End Sub |
沒有留言:
張貼留言