巨大的VBA功能问题



我正在制作一个电子表格,该电子表格将获取从工作中输入的数据并在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 实例都有一个单元格级数。 在每个子

感谢所有提供帮助的人。瑞克。

为此,请将主宏分解为较小的部分,并确保声明将在Module级别的多个Sub Procedures中使用的变量(例如,在任何Sub Procedure之外的Module的最顶部)。

接下来,创建一个单独的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宏分配给按钮。

最新更新