返回列表 上一主題 發帖

[發問] 請問如何用vba來將模組或代碼寫入另一個excel

回復 3# starry1314

給你一個大概的模型,其他的請自行修改內容
以下代碼必須在工具>>設定引用項目中,引用 Microsoft Visual Basic For Application Extensibility 5.3<<這個要勾選
  1. Option Explicit
  2. Public Sub ex()
  3.     Dim s As String
  4.     Dim VBCom As Object
  5.     Dim VBP
  6.     On Error Resume Next
  7.      Do
  8.         Err = 0
  9.         Set VBP = ActiveWorkbook.VBProject
  10.         If Err <> 0 Then
  11.             If MsgBox("巨集安全設置置不允許代代碼進行行操作。" & vbCrLf & vbCrLf & "請將信任中心內信任存取VBA專案物件模型勾選", vbCritical + vbYesNo, "巨集設定") = vbYes Then
  12.                 With Application
  13.                     .SendKeys "t"
  14.                     .CommandBars.FindControl(ID:=3627).Execute
  15.                 End With
  16.             Else
  17.                 Exit Sub
  18.             End If
  19.         End If
  20.     Loop Until Err = 0
  21.     On Error GoTo 0
  22.     s = "sub 合計()" & vbCrLf '
  23.     s = s & "    Dim rng1 As Range, rng2 As Range" & vbCrLf
  24.     s = s & "    Dim 日期 As Date" & vbCrLf
  25.     s = s & "    With Sheets(1)" & vbCrLf '
  26.     s = s & "        號碼 = .Cells(3, 18)" & vbCrLf
  27.     s = s & "        日期 = .Cells(2, 19)" & vbCrLf
  28.     s = s & "        Set rng1 = .Columns(1).Find(號碼)" & vbCrLf
  29.     s = s & "        Set rng2 = .Rows(1).Find(日期, LookIn:=xlValues)" & vbCrLf
  30.     s = s & "        .Cells(3, 19) = .Cells(rng1.Row, rng2.Column).Value" & vbCrLf
  31.     s = s & "    End With" & vbCrLf '
  32.     s = s & "End Sub"
  33.     Set VBCom = ThisWorkbook.VBProject.VBComponents.Add(1) '插入模組
  34.     VBCom.Name = "模組1" '變更模組名稱
  35.     With VBCom.CodeModule
  36.         .InsertLines .CountOfLines + 1, s '寫入代碼
  37.     End With
  38. End Sub
複製代碼

TOP

回復 3# starry1314

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

TOP

        靜思自在 : 改變自己是自救,影響別人是救人。
返回列表 上一主題