JoshL
JoshL

Reputation: 164

Calling another sub within a different sub

I have a sub to fill a pdf depending on which element is up. The first sub is a routine to actually open the pdf and fill it with info. The second sub is actually finding which sub to run depending on the element that it reads.

The final line in the second sub is where I trying to call my first sub. This is always bringing up an error [400].

The following code is my first sub routine:

   Sub FSCEIEO()

   Dim PDFTemplateFile, NewPDFName, SavePDFFldr, Desc As String
   Dim CustRow, LastRow As Long

    With Sheet2

    D1 = .Range("AB" & CustRow).Value & "'' Diameter x " & .Range("AD" & CustRow).Value & "''"
    D2 = .Range("AS" & CustRow).Value
    D3 = .Range("AZ" & CustRow).Value & " lbs"
    D4 = .Range("S" & CustRow).Value
    D5 = .Range("AO" & CustRow).Value & "''"
    D6 = .Range("AR" & CustRow).Value & "''"
    D7 = .Range("AP" & CustRow).Value & "''"
    D8 = .Range("AK" & CustRow).Value & "''"

    Description = D4

    DataEntry = Array(D1, D2, D3, D4, D5, D6, D7, D8)

    For Each e In DataEntry

    Application.SendKeys "{Tab}", True
    Application.SendKeys e, True
    Application.Wait Now + 0.00001

    Next e

    Application.SendKeys "{Tab}", True
    Application.SendKeys "{Esc}", True

    Application.SendKeys "^(p)", True
    Application.Wait Now + 0.00001

    Application.SendKeys "{Tab}", True
    Application.SendKeys "{Enter}", True
    Application.Wait Now + 0.00001
    Application.SendKeys "{l}", True
    Application.SendKeys "{Enter}", True
    Application.Wait Now + 0.00001
    Application.SendKeys "{Left}", True
    Application.SendKeys "{Enter}", True

    Application.SendKeys "{Enter}", True
    Application.Wait Now + 0.00001

    If Dir(SavePDFFldr & "\" & Description & ".pdf") <> Empty Then Kill (SavePDFFldr & "\" & Description & ".pdf")

    Application.SendKeys SavePDFFldr & "\" & Description & ".pdf"
    Application.Wait Now + 0.00001

    Application.SendKeys "%(s)"
    Application.Wait Now + 0.00001


    Application.SendKeys "^(q)", True
    Application.SendKeys "{numlock}%s", True
    Application.SendKeys "{Tab}", True
    Application.SendKeys "{Enter}", True

   End With
   End Sub

My second sub is as follows:

Sub CreateDrawings()
Dim PDFTemplateFile, NewPDFName, SavePDFFldr, Desc As String
Dim CustRow, LastRow As Long

LastRow = Sheet2.Range("A999").End(xlUp).Row

                                           ''''''''''''''''''''''''''''''''''''''''''''    PDF TEMPLATES     ''''''''''''''''''''''''''''''''''''''''''''''''''''

PDFTemplateFile1 = Sheet4.Range("BU3").Value    ' FSC EIEO
PDFTemplateFile2 = Sheet4.Range("BU4").Value    ' FSC EISO
PDFTemplateFile3 = Sheet4.Range("BU5").Value    ' FSC SIEO
PDFTemplateFile4 = Sheet4.Range("BU6").Value    ' FSC SISO
PDFTemplateFile5 = Sheet4.Range("BU7").Value    ' NBG
PDFTemplateFile6 = Sheet4.Range("BU8").Value    ' RAIN CAPS
PDFTemplateFile7 = Sheet4.Range("BU9").Value    ' CPMS Single
PDFTemplateFile8 = Sheet4.Range("BU10").Value   ' CPMS Dual
PDFTemplateFile9 = Sheet4.Range("BU11").Value   ' Expansion Joint
PDFTemplateFile10 = Sheet4.Range("BU12").Value
PDFTemplateFile11 = Sheet4.Range("BU13").Value
PDFTemplateFile12 = Sheet4.Range("BU14").Value
PDFTemplateFile13 = Sheet4.Range("BU15").Value
PDFTemplateFile14 = Sheet4.Range("BU16").Value
PDFTemplateFile15 = Sheet4.Range("BU17").Value
PDFTemplateFile16 = Sheet4.Range("BU18").Value

SavePDFFldr = Sheet2.Range("Q47").Value

With Sheet2

For CustRow = 5 To 6
Application.Wait Now + 0.00001

                                           ''''''''''''''''''''''''''''''''''''''''''''    FSC EIEO     ''''''''''''''''''''''''''''''''''''''''''''''''''''

If InStr(Sheet2.Range("R" & CustRow), "FSC") > 0 Then
If Sheet2.Range("AQ" & CustRow) = "EIEO" Then
   ThisWorkbook.FollowHyperlink PDFTemplateFile1
    Application.Wait Now + 0.00001
   FSCEIEO
    End With
End Sub

Upvotes: 0

Views: 54

Answers (1)

Elbert Villarreal
Elbert Villarreal

Reputation: 1716

Sub firstSub()
    MsgBox "OK"
End Sub

Sub secondSub()
    Application.Run "firstSub"
End Sub

Use Application.Run "yourSub" to run another sub, and use Application.Run "yourModule.YourSub" if the Sub is in another module

Your code could be like this:

   With Sheet2     
      For CustRow = 5 To 6
         Application.Wait Now + 0.00001 
         '''''''''''''''''''''''''''''''''''  
            If InStr(Sheet2.Range("R" & CustRow), "FSC") > 0 Then
            If Sheet2.Range("AQ" & CustRow) = "EIEO" Then
               ThisWorkbook.FollowHyperlink PDFTemplateFile1
               Application.Wait Now + 0.00001
               Application.Run "FSCEIEO" '
               'If your Sub is in another module
               'Application.Run "ModuleName.FSCEIEO"
            End if 'Change this part
         End If 'Your missing this closing IF
   End With 'Check your closing tags

And maybe the Error[400] could it be because this missing End If About the Application.Run, you can use it (call the sub) even if te SUB is defined as Private. See this for more info : Application.Run

Upvotes: 1

Related Questions