我正在制作一个电子表格,该电子表格将从工作中输入数据并在 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 实例都有一个单元格进程。在每个子
感谢所有帮助过的人。瑞克。