?
Solved

Check for file during archive - vb from excel

Posted on 2008-06-16
4
Medium Priority
?
141 Views
Last Modified: 2010-05-03
I have a macro which copies files from one directory to another only if the following condition exists:
Costs*.xls.  If there are no files in the source directory, the vb script produces an error message that I do not want the users to see.  Is there any way to display a message box that says there are no files that meet the archive criterion?  I have attached my macro for your review.
Sub ArchiveFiles()
    Dim fso As Object
    
    Dim sSourceDir As String
    Dim sTargetDir As String
    
    sSourceDir = "C:\my documents\"
    sTargetDir = "C:\my documents\backup\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    fso.movefile Source:=sSourceDir & "Costs*.xls", Destination:=sTargetDir
    fso.movefile Source:=sSourceDir & "Status*.xls", Destination:=sTargetDir
    
End Sub

Open in new window

0
Comment
Question by:ilrosebud26
[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
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 500 total points
ID: 21794713
The filesystemobject doesn't have a files collection that uses wildcards as far as I know - so, you'd first have to test using dir$, e.g.,

If Len(Dir$(fso.buildpath(sSourceDir, "Costs*.xls")) > 0 then

   ' do the move.

    fso.movefile Source:=sSourceDir & "Costs*.xls", Destination:=sTargetDir

else

    MsgBox "No matching files"

endif
0
 

Author Comment

by:ilrosebud26
ID: 21794831
Peetm-
    Thanks, I will try this after lunch and let you know.

Rosemary
0
 

Author Comment

by:ilrosebud26
ID: 21797390
That worked great!

Thanks!
Rosemary

0
 

Author Closing Comment

by:ilrosebud26
ID: 31467655
Thanks for getting back so quickly.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

719 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