+ Reply to Thread
Results 1 to 6 of 6

DIR function

  1. #1
    Registered User
    Join Date
    06-01-2020
    Location
    Scotland
    MS-Off Ver
    2004
    Posts
    4

    DIR function

    Hello

    I am fairly new to VBA, however I have an excel spreadsheet that contains various addresses.

    Each address has a folder . For each address there should be 3 files. The files have their own abbreviations I.e the picture file has PIC at the end. Each file is named in the format address_abbreviation I.e 1ABCStreet_PIC.

    I have been trying to identify if any files are missing using couple of options:

    1. Function FileExists(sFile As String)
    sPath = “Folder location” & sFile & “_PIC.pdf”
    FileExists = DIR(sPath) <> “”
    End Function

    Then for each address in the spreadsheet I have been inserting the formula =If(FileExists(A1), “y”, “n”). Where A1 has the address in the same format as that the files have been named.

    It always returns “n” and I am unsure where I am going wrong.

    2. Sub DIR()
    Dim Filename As String
    Filename=“Folder location” & A1 & “_PIC.pdf”
    If Filename = VBA.Constants.vbNullString then
    Sheet1.Range(“B1”).Value = “n”
    Else
    Sheet1.Range(“B1”).Value=“y”
    End If
    End sub

    I believe there is an issue with trying to specify the filename using data from excel cells, but that is just a guess and any help would be much appreciated.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: DIR function

    Hi, welcome to the forum, the return value of you macro is a Boolean, either TRUE or FALSE
    and not y or n
    You will have to create that yourself

    Please Login or Register  to view this content.
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    06-01-2020
    Location
    Scotland
    MS-Off Ver
    2004
    Posts
    4
    So I updated the VBA function to
    FunctionExists (sFile As String) As Boolean
    sPath = “Folder location” & sFile & “PIC.pdf”
    If FileExists = DIR(sPath) <> “” Then
    FileExists = True
    Else
    FileExists = False
    End If
    End Function

    The formula in the worksheet is now just = FileExists(A1)

    But it returns #Value!

    😒

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: DIR function

    Sure it does, you mistyped the function check your syntax FUNCTIONEXITS ?????
    You didn't need to change the code

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: DIR function

    What are the values for
    “Folder location”
    sFile & “_PIC.pdf”
    ?

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: DIR function

    This is the synxat
    Please Login or Register  to view this content.
    And how it looks in the formula
    Attached Images Attached Images

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 07-07-2018, 04:13 AM
  2. COUNTUNIQUE Function in Google Sheets; Excel lacks a direct counterpart to this function?
    By PivotTablePSHomage in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-18-2018, 05:27 AM
  3. Replies: 3
    Last Post: 08-14-2017, 06:26 AM
  4. Calling function inside function. (aka nested function)
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2015, 05:58 AM
  5. Replies: 13
    Last Post: 04-08-2014, 05:46 AM
  6. [SOLVED] Using Offset function as the array in the PercentRank function is giving wrong result
    By Bobneil in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 08-06-2013, 09:29 PM
  7. Replies: 1
    Last Post: 03-21-2012, 11:22 AM

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