Rick Boyle
Rick Boyle

Reputation: 1

Huge VBA Function problems

I am working on a spreadsheet that will take data inputted from a job and create an executive summary in MS Word. I have programmed in all of the code required to get the numbers and verbiage correct in the word document, but the VBA Function is too large.

Is there a way I can split it up in to several smaller Functions that all execute with one button in excel?

Edit. As recommended below I have set the following.

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

It will get as far as running the master Function then throw the following error. "Compile Error Invalid or unqualified reference"

Hopefully this edit will get my topic back on track and open for help. I will admit to being an amateur with coding.

The code for data repeats itself 10 times with one cell progression for each of the .range instances. in each sub

Thank you to all who have helped. Rick.

Upvotes: 0

Views: 304

Answers (1)

ARich
ARich

Reputation: 3279

To do this, break your main macro up into smaller sections and make sure to declare variables that will be used in multiple Sub Procedures at the Module level (e.g. at the very top of the Module outside of any Sub Procedure).

Next, create a separate Sub Procedure to call each new procedure. It can be in the same Module or in a different one, but it must stay inside the same VBA Project.

Sub CallMacros()

Call TheNameOfTheFirstMacroYouWantToCall
Call TheNameOfTheSecondMacroYouWantToCall

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

End Sub

Assign the CallMacros macro to your button.

Upvotes: 1

Related Questions