Consulting

Results 1 to 5 of 5

Thread: Calling a function

  1. #1
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location

    Calling a function

    I am trying to call a function from a sub routine but cannot do it. Error: argument not optional. I've researched in a lot of places to find a solution but I don't really understand the answers people give.

    The function I'm using checks whether a file already exists:

    Function FileExist(FilePath As String) As Boolean

    I want to call this function from another macro. Using "Call FileExist" doesn't work, what will?

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Try:
    dim ReturnCode
    ReturnCode = FileExist("c:\test.txt")
    msgbox ReturCode

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by DeanP View Post
    I am trying to call a function from a sub routine but cannot do it. Error: argument not optional. I've researched in a lot of places to find a solution but I don't really understand the answers people give.

    The function I'm using checks whether a file already exists:

    Function FileExist(FilePath As String) As Boolean

    I want to call this function from another macro. Using "Call FileExist" doesn't work, what will?

    In the definition

    Function FileExist(FilePath As String) As Boolean

    FilePath is the 'Argument'. 'Not Optional' means that it is required

    Since it is supposed to determine if a file exists, you have to tell it which file you want to check

    If FileExists ("C:\Users\MyName\Documents\Report.xlsx") Then
    '   do if true
    Else
    '   do if not there
    End IF

    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Dim doesFileExist as Boolean
    doesFileExist=FileExist("C:\SomeFileName.txt")

  5. #5
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location
    Thank you all for your help!

Posting Permissions

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