+ Reply to Thread
Results 1 to 7 of 7

VBA code: A Sub() module to CALL another Sub ()

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Post VBA code: A Sub() module to CALL another Sub ()

    Hello experts!

    I need some expert advice here please....

    I have a SaveAsPDF module created that when run, it needs to convert the worksheet into excel. But before doing that, I need another module to run first, that is to check whether certain required fields are filled or not before it converts to .pdf. Pasted are my codes for both modules. I tried calling the Checkcells module but I keep on receiving "Compile error: Argument not optional" message. Or is there a way to incorporate my codes in one module that does both functions?

    Please have time to assist. Thanks a lot in advance!

    Sub SaveAsPDF()
        
    Call Checkcells.emptycell
        
        Dim fname As String
        Dim wb As Workbook
        Dim newfilename As String
        Dim newfilefilter As String
        Dim mytitle As String
            
        Set wb = ThisWorkbook
        
        Cells.Interior.ColorIndex = 0
        newfilename = "Shift Handover" & "_" & wb.Sheets("Shift Handover_D2").Range("E8").Value & "_" & wb.Sheets("Shift Handover_D2").Range("E9") & ".pdf"
        newfilefilter = "PDF files (*.pdf), *.pdf"
        
        mytitle = "Navigate to the required folder"
        
        fname = Application.GetSaveAsFilename(InitialFileName:=newfilename, Filefilter:="PDF files (*.pdf), *.pdf")
        If fname <> "False" Then
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=newfilename, OpenAfterPublish:=True
        Else
        MsgBox ("File NOT converted. Make sure to convert before exit."), vbExclamation
        End If
    End Sub

    Public Sub emptycell(cancel As Boolean)
    Dim ws As Worksheet
    
    Set ws = Sheets("Shift Handover_D2")
    
    If ws.Range("c4") = Empty Then
    ws.Range("c4").Interior.ColorIndex = 38
    MsgBox ("Please enter Station Name")
    cancel = True
    Else
    ws.Range("c4").Select
    ws.Range("c4").Interior.ColorIndex = 0
    End If
        If ws.Range("e7") = Empty Then
        ws.Range("e7").Interior.ColorIndex = 38
        MsgBox ("Please enter Outgoing shift schedule.")
        cancel = True
        Else
        ws.Range("e7").Interior.ColorIndex = 0
        End If
            If ws.Range("i7") = Empty Then
            ws.Range("i7").Interior.ColorIndex = 38
            MsgBox ("Please enter Incoming shift schedule.")
            cancel = True
            Else
            ws.Range("i7").Interior.ColorIndex = 0
            End If
                If ws.Range("f19") = Empty Then
                ws.Range("f19").Interior.ColorIndex = 38
                MsgBox ("Please enter Next Access Number.")
                cancel = True
                Else
                ws.Range("f19").Interior.ColorIndex = 0
                End If
    End Sub
    Thanks again in advance!

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: VBA code: A Sub() module to CALL another Sub ()

    What happens if you put your PDF code at the end of the emptycell code to make it one macro?

  3. #3
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: VBA code: A Sub() module to CALL another Sub ()

    Quote Originally Posted by jolivanes View Post
    What happens if you put your PDF code at the end of the emptycell code to make it one macro?
    Hi jolivanes

    Tried your suggestion but it still shows same error: "Compile error: Argument not optional"

  4. #4
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: VBA code: A Sub() module to CALL another Sub ()

    Quote Originally Posted by hydz1213 View Post
    Hi jolivanes

    Tried your suggestion but it still shows same error: "Compile error: Argument not optional"
    CORRECTION TO ME! I tried it again....paste my PDF code under the EMPTYCELL code and it did run which is really good.
    However, when I emptied some of the specified cells (C4,E7) it still converts the worksheet into .pdf

    I was hoping to have a code that the macro will check first if there are empty cells among specified cells. If yes, it will just highlight empty cells and remind user to fill-in the form first and PDF will not run. But if all specified cells are filled-out, then macro will convert worksheet into .pdf....

  5. #5
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: VBA code: A Sub() module to CALL another Sub ()

    Quote Originally Posted by jolivanes View Post
    What happens if you put your PDF code at the end of the emptycell code to make it one macro?
    Thanks also to you jolivanes

  6. #6
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA code: A Sub() module to CALL another Sub ()

    I think you should change the call to CheckCells from the SaveAsPDF routine

    currently you have

    Sub SaveAsPDF()
        
    Call Checkcells.emptycell
        
        Dim fname As String
        Dim wb As Workbook
    replace the highlighted part by

    '# declare
        dim returnval as boolean
    '# call the routine to check if all cells have been filled, if not returnval is returned as TRUE
        call emptycell(returnval)
    '# exit the routine (i.e. do not create the PDF) if one or more fields were foudn to be empty
        If returnval then 
            exit sub
        end if
    If you like my contribution click the star icon!

  7. #7
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: VBA code: A Sub() module to CALL another Sub ()

    [QUOTE=OllieB;3115871]I think you should change the call to CheckCells from the SaveAsPDF routine

    AWESOME!!! It worked Thank you so much for your help! I wonder how can I tag this as SOLVED???

    But super thanks to your help OllieB

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1