Check file exist vba
Results 1 to 5 of 5

Thread: Check file exist vba

  1. #1
    Board Regular
    Join Date
    Oct 2007
    Posts
    723
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Check file exist vba

    I'm using MKDir to create a file in a folder. When creating a file, the file name is saved with a random generated 16-digits Alpha-Numeric (i.e. DS1O-Q7LN-X7MP-EJA8) name. The code below checks for a specific file, however How can i check if there is a 16-digits Alpha-Numeric file exist?

    Code:
    Function FileExists(FilePath As String) As Boolean
    Dim TestStr As String
        TestStr = ""
        On Error Resume Next
        TestStr = Dir(FilePath)
        On Error GoTo 0
        If TestStr = "" Then
            FileExists = False
        Else
            FileExists = True
        End If
    End Function
    The trick with sneaky users is not teaching them anything lest they get wise!

  2. #2
    MrExcel MVP AlphaFrog's Avatar
    Join Date
    Sep 2009
    Posts
    16,043
    Post Thanks / Like
    Mentioned
    16 Post(s)
    Tagged
    7 Thread(s)

    Default Re: Check file exist vba

    You want to check if any 16 character filename exists in a given folder?

    Code:
    Function FileExists(FilePath As String) As Boolean
        
        If Right(FilePath, 1) <> "\" Then FilePath = FilePath & "\"
        
        FileExists = Len(Dir(FilePath & "????-????-????-????.*")) > 0
        
    End Function
    Paste your Excel data to the forum...
    MrExcel HTML Maker or Excel Jeanie

    How to post your vba code
    [CODE]your VBA code here[/CODE]
    The # button in the forum's editor will apply CODE tags around your selected text.

  3. #3
    Board Regular
    Join Date
    Oct 2007
    Posts
    723
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Check file exist vba

    Works Great!! Thank you AlphaFrog
    The trick with sneaky users is not teaching them anything lest they get wise!

  4. #4
    Board Regular
    Join Date
    Oct 2007
    Posts
    723
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Check file exist vba

    If a file exist in the described format above, How can I insert the file name in cell? I tried the code below, but it not entering the correct file name.

    Code:
    Range("A1") = Len((Dir(OPath) & "????-????-????-????.*"))
    Last edited by Av8tordude; Aug 27th, 2019 at 04:21 AM.
    The trick with sneaky users is not teaching them anything lest they get wise!

  5. #5
    MrExcel MVP AlphaFrog's Avatar
    Join Date
    Sep 2009
    Posts
    16,043
    Post Thanks / Like
    Mentioned
    16 Post(s)
    Tagged
    7 Thread(s)

    Default Re: Check file exist vba

    Range("A1") = Dir(OPath & "????-????-????-????.*")
    Paste your Excel data to the forum...
    MrExcel HTML Maker or Excel Jeanie

    How to post your vba code
    [CODE]your VBA code here[/CODE]
    The # button in the forum's editor will apply CODE tags around your selected text.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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