0

我正在制作一个电子表格,该电子表格将从工作中输入数据并在 MS Word 中创建执行摘要。我已经编写了在 word 文档中获得正确数字和措辞所需的所有代码,但是 VBA 函数太大了。

有没有一种方法可以将其拆分为几个较小的功能,这些功能都可以在 excel 中通过一个按钮执行?

编辑。按照下面的建议,我设置了以下内容。

Sub LazyEngineer()

Call Master
Call data
Call data1
Call data2
Call data3

End Sub

Sub Master()

    Dim appWD As Word.Application

    Set appWD = CreateObject("Word.Application")
    appWD.Visible = True
    appWD.Documents.Add
    Myfilename = appWD.ActiveDocument.Name
    appWD.PrintPreview = True
    With appWD.ActiveWindow.ActivePane.View.Zoom
        .PageColumns = 1
        .PageRows = 1
    End With
    'header
  -- lots of other code to make this thing type a report --
 End Sub

Sub data()
 '1
     If ThisWorkbook.Sheets("Job Data").Range("b31") > 0 Then
         .TypeText "    Stage "
         .TypeText ThisWorkbook.Sheets("Job Data").Range("b5")
         .TypeParagraph
         .TypeText " The well had an initial pressure of "
         .TypeText ThisWorkbook.Sheets("Job Data").Range("b7")
         .TypeText "psi."
         .TypeText " The well broke down at "
         .TypeText ThisWorkbook.Sheets("job Data").Range("b11")
         .TypeText "psi at "
         .TypeText ThisWorkbook.Sheets("Job Data").Range("b12")
         .TypeText "bpm, a total of "
         .TypeText ThisWorkbook.Sheets("Job Data").Range("b31")
         .TypeText " clean bbls of fluid was pumped. The Treatment was pumped to completion."
    If ThisWorkbook.Sheets("job Data").Range("A28") > 0 Then
            .TypeText "The total amount of proppant pumped was "
            .TypeText ThisWorkbook.Sheets("Job Data").Range("b28")
            .TypeText " lbs of "
            .TypeText ThisWorkbook.Sheets("Job Data").Range("a28")
            .TypeText ", "
            End If
   If ThisWorkbook.Sheets("job Data").Range("A29") > 0 Then
        .TypeText ThisWorkbook.Sheets("Job Data").Range("b29")
            .TypeText " lbs of "
            .TypeText ThisWorkbook.Sheets("Job Data").Range("a29")
            .TypeText ", "
            End If
  If ThisWorkbook.Sheets("job Data").Range("A30") > 0 Then
        .TypeText ThisWorkbook.Sheets("Job Data").Range("b30")
            .TypeText " lbs of "
            .TypeText ThisWorkbook.Sheets("Job Data").Range("a30")
            End If
    .TypeText ". The average pressure and rate were "
    .TypeText ThisWorkbook.Sheets("Job Data").Range("B23")
    .TypeText "psi and "
    .TypeText ThisWorkbook.Sheets("Job Data").Range("b24")
    .TypeText "bpm. "
    If ThisWorkbook.Sheets("Job Data").Range("B19") > 0 Then
    .TypeText "The Initial ISIP was "
    .TypeText ThisWorkbook.Sheets("Job Data").Range("B19").Text
    .TypeText "psi ("
    .TypeText ThisWorkbook.Sheets("Job Data").Range("b20").Text
    .TypeText " psi/ft)."
    End If
    .TypeText " The final ISIP was "
    .TypeText ThisWorkbook.Sheets("Job Data").Range("b21").Text
    .TypeText " psi ("
    .TypeText ThisWorkbook.Sheets("Job Data").Range("b22").Text
    .TypeText " psi/ft)."
   End If
    .TypeParagraph
 End Sub

 Sub data1()
  --  Code --
 End Sub

 Sub data2()
  --  Code --
 End Sub

 Sub data3()
  --  Code --
 End Sub

它将尽可能运行主函数,然后抛出以下错误。“编译错误无效或不合格的参考”

希望这次编辑能让我的话题回到正轨并寻求帮助。我承认我是一个编码爱好者。

数据代码重复 10 次,每个 .range 实例都有一个单元格进程。在每个子

感谢所有帮助过的人。瑞克。

4

1 回答 1

1

为此,请将您的主宏分解为更小的部分,并确保声明将Sub Procedures在该级别中以多个方式使用的变量(例如,在any 外部的Module最顶部)。ModuleSub Procedure

接下来,创建一个单独Sub Procedure的来调用每个新过程。它可以在同一个Module或不同的一个,但它必须保持在同一个VBA Project.

Sub CallMacros()

Call TheNameOfTheFirstMacroYouWantToCall
Call TheNameOfTheSecondMacroYouWantToCall

'Continue calling the smaller macros until you've called them all.

End Sub

CallMacros宏分配给您的按钮。

于 2013-10-17T14:29:19.617 回答