我正在处理一个电子表格,该表格将从作业输入数据并在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
它将运行master函数然后抛出以下错误。 “编译错误无效或不合格的参考”
希望这次编辑能让我的话题回到正轨并开放寻求帮助。我承认自己是一名编码的业余爱好者。
对于每个.range实例,数据代码重复10次,每个细胞进展一次。在每个子
感谢所有帮助过的人。 瑞克。
答案 0 :(得分:1)
要执行此操作,请将主宏分成较小的部分,并确保声明将在Sub Procedures
级别的多个Module
中使用的变量(例如,在{{的最顶部) 1}}在任何Module
之外。
接下来,创建一个单独的Sub 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
宏指定给您的按钮。