Option Explicit
Public Sub ex()
Dim s As String
Dim VBCom As Object
Dim VBP
On Error Resume Next
Do
Err = 0
Set VBP = ActiveWorkbook.VBProject
If Err <> 0 Then
If MsgBox("巨集安全設置置不允許代代碼進行行操作。" & vbCrLf & vbCrLf & "請將信任中心內信任存取VBA專案物件模型勾選", vbCritical + vbYesNo, "巨集設定") = vbYes Then
With Application
.SendKeys "t"
.CommandBars.FindControl(ID:=3627).Execute
End With
Else
Exit Sub
End If
End If
Loop Until Err = 0
On Error GoTo 0
s = "sub 合計()" & vbCrLf '
s = s & " Dim rng1 As Range, rng2 As Range" & vbCrLf
s = s & " Dim 日期 As Date" & vbCrLf
s = s & " With Sheets(1)" & vbCrLf '
s = s & " 號碼 = .Cells(3, 18)" & vbCrLf
s = s & " 日期 = .Cells(2, 19)" & vbCrLf
s = s & " Set rng1 = .Columns(1).Find(號碼)" & vbCrLf
s = s & " Set rng2 = .Rows(1).Find(日期, LookIn:=xlValues)" & vbCrLf
s = s & " .Cells(3, 19) = .Cells(rng1.Row, rng2.Column).Value" & vbCrLf
s = s & " End With" & vbCrLf '
s = s & "End Sub"
Set VBCom = ThisWorkbook.VBProject.VBComponents.Add(1) '插入模組
VBCom.Name = "模組1" '變更模組名稱
With VBCom.CodeModule
.InsertLines .CountOfLines + 1, s '寫入代碼
End With
End Sub