Solved

Check for file during archive - vb from excel

Posted on 2008-06-16
4
134 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
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
peetm earned 125 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now