矩陣計算:矩陣相加
矩陣是?參WIKI
筆者日前分享了Application.InputBox
利用機會結合一下矩陣計算,做VBA應用分享。
步驟:
1.先做一個VBA,ActiveX按鈕,如圖1.,VBA code:
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
'COPYRIGHT BY dropit.liu. | |
Private Sub CommandButton1_Click() | |
A_INPUTBOX_address = Application.InputBox("", Type:=8).Address '取得位置 | |
A_INPUTBOX = ActiveSheet.Range(A_INPUTBOX_address).Value '取得資料 | |
b_INPUTBOX_address = Application.InputBox("", Type:=8).Address '取得位置 | |
b_INPUTBOX = ActiveSheet.Range(b_INPUTBOX_address).Value '取得資料 | |
A_CHECK = UBound(A_INPUTBOX) | |
B_CHECK = UBound(b_INPUTBOX) | |
If A_CHECK <> B_CHECK Then | |
MsgBox "矩陣不對稱" | |
End If | |
For i = LBound(A_INPUTBOX) To UBound(A_INPUTBOX) Step 1 | |
For j = LBound(A_INPUTBOX) To UBound(A_INPUTBOX) Step 1 | |
A_INPUTBOX(i, j) = A_INPUTBOX(i, j) + b_INPUTBOX(i, j) | |
Next j | |
Next i | |
Call sheet_name_check_delete("矩陣相加結果") | |
Call 矩陣輸出("矩陣相加結果") | |
End Sub |
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
'COPYRIGHT BY dropit.liu. | |
Sub sheet_name_check_delete(name_check) '若sheet已存在則刪除 | |
For I = 1 To Sheets.Count Step 1 | |
If Sheets(I).Name = name_check Then | |
Sheets(I).Delete | |
exit for | |
End If | |
Next I | |
End Sub |
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
'COPYRIGHT BY dropit.liu. | |
Sub 矩陣輸出(SHEET_NAME) | |
Sheets.Add after:=Sheets(Sheets.Count) | |
Sheets(Sheets.Count).Name = SHEET_NAME | |
Sheets(SHEET_NAME).Range(A_INPUTBOX_address) = A_INPUTBOX '寫入資料 | |
Sheets(SHEET_NAME).Range(A_INPUTBOX_address).Interior.Color = QBColor(14) '反黃儲存格 | |
End Sub |
1.先選擇放有矩陣的儲存格(A、B兩個矩陣選擇對話框);
圖1.
2.自動產生"矩陣相加結果"工作表,作為結果輸出。
圖3.結果輸出
沒有留言:
張貼留言