Solved

How do I check for an existing file in VBA Excel ?

Posted on 2014-09-11
11
229 Views
Last Modified: 2014-09-15
Hi All,

I am building a small Excel VBA application and I want to check if a Windows folder has a file in it. If it does then I want to process the file by copying it to another folder and then deleting it.

How should I begin to code this ?

Thanks
Toco
0
Comment
Question by:Tocogroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40316835
This is one way:
If Dir("C:\Test\Test.txt") = "" Then
  MsgBox "File does not exist"
Else
  MsgBox "File exist"
End If

Open in new window

0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 250 total points
ID: 40316842
This is another:
Dim oFso As Object

Set oFso = CreateObject("Scripting.FileSystemObject")

If oFso.FileExists("C:\Test\Test.txt") = False Then
     MsgBox "File does not exist"
Else
    MsgBox "File exist"
End If

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40316848
Another way:

Sub test()

Set fs = CreateObject("Scripting.FileSystemObject")

If fs.FileExists("c:\a.txt") Then
    fs.CopyFile "c:\a.txt", "c:\temp\a.txt"
    fs.DeleteFile "c:\a.txt"
End If

End Sub

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:Tocogroup
ID: 40316874
Sorry, my fault. I should have said a folder with 'any' file in it. That is, I don't know the filename.
0
 

Author Comment

by:Tocogroup
ID: 40316949
So, I want to check to see if ANY file exists in that folder and if it does then I copy and delete it. How do I acquire its filename ?
0
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 250 total points
ID: 40317153
You loop through it here:

Sub test()

Set fs = CreateObject("Scripting.FileSystemObject")

Source = "c:\temp1"
Dest = "c:\temp2"
Set r = fs.GetFolder(Source)

For Each f In r.Files
    fs.CopyFile Source & "\" & f.Name, Dest & "\" & f.Name
    fs.DeleteFile Source & "\" & f.Name
Next


End Sub

Open in new window

0
 

Author Comment

by:Tocogroup
ID: 40317220
I'm getting a 'RunTime error  Path not found' message on the last line of the script that reads as follows:
Source = "\Input"
Dest = "\Archive"
Set r = fs.GetFolder(Source)

Is this because it doesn't use relative folder addresses ? Do I have to enter the full file path ? Or can I use a relative address ?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 40317221
NFP:

You can just replace the Test.txt file name in MacroShadow's Dir code with *.*
0
 

Author Comment

by:Tocogroup
ID: 40317245
'RunTime error 76 Path not found' I meant to say.
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40317301
Please use FULL path!
0
 

Author Closing Comment

by:Tocogroup
ID: 40322630
Thanks to you both for assisting me with this. Consequently, I've split the points.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question