Consulting

Results 1 to 5 of 5

Thread: Get File Extension

  1. #1

    Question Get File Extension

    Hi, I would like code for a macro function that does the following:

    Looks in a specified file, (S:\filepath\filename), without knowing the extension of the file, and displays the extension for the first file found with a matching file name.

    For instance:
    In cell A1 the user would type: '600001' (file name) and in cell A2 there would be the function '=ExtFind(A1)' and it would display '.xls'. The file path will always be the same: S:\Data\ManEng\BOS\MFGENG\
    If there is no file found, it would be nice if A2 displayed "No File"

    Can anyone help me? I have been trying to figure this out for a long time.
    Thanks

  2. #2
    I am using Excel 2003.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Function ExtFind(filename)
    Const FilePath As String = "S:\Data\ManEng\BOS\MFGENG\"
    Dim sFile As String

    sFile = Dir(FilePath & filename & "*")
    If sFile = "" Then
    ExtFind = "No file"
    Else
    ExtFind = Right$(sFile, Len(sFile) - InStrRev(sFile, "."))
    End If
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Thank you SOO much. Thats perfect.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can also use Split for getting parts of a file path. Very flexible, with a bit of practice.
    [VBA]
    ExtFind = Split(filename, ".")(UBound(Split(filename, ".")))

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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