Results 1 to 2 of 2
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am having a challenge where I need to include two searches for file names in my VBA code.

    The code does like like the 2nd reference to the DIR statement and errors out.

    [codebox]Example:
    strFileName = Dir(MyFirstPath & MyFile)

    While strFileName <> ""

    'Change File Attributes/Delete existing File
    Results = Dir(MyPath & MyFile)
    If Results = "" Then
    'Do Nothing
    Else
    SetAttr "" & MyPath & MyFile & "",vbNormal
    Kill MyPath & MyFile
    End If

    'Now copy newer file to MyPath
    Run some code to copy newer file to MyPath

    WEnd
    [/codebox]


    Your suggestions are appreciated,
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You cannot use Dir with a different path within a Dir loop - it messes up the Dir loop. You could store the filenames from the Dir loop in an array, then loop through the items of the array. You can safely use Dir within the second loop.

    BTW you need a line

    strFileName = Dir

    just above WEnd.

Posting Permissions

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