Excel Check for file before executing renainder of marco from Mr Excel


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

Check for file before executing renainder of marco
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

Trag
Board Regular


Joined: 18 Dec 2003
Posts: 46
Location: Vancouver
Flag: Canada

Status: Offline

 Reply with quote  

Check for file before executing renainder of marco

I'm trying to figure out how I can check to see if my file exists before my macro continues but I'm having difficulties.

Sub Check_To_See_If_File_Exists()
If Dir("C:\Documents and Settings\Trag\Desktop\Test.xls") = "" Then
MsgBox(vbOKOnly, "NO FILE EXISTS")
End
End If
Workbooks.Open Filename:="C:\Documents and Settings\Trag\Desktop\Test.xls"

'The rest of my macro goes here...

End Sub

Could someone be so kind to tell me where I'm going wrong? I'm a little lost but I've tried.

I need to have my marco check to see if the file exists before it can go on. If it can't go one, msgbox "No file Exists", macro stops.

Post Thu Mar 25, 2004 8:29 am 
 View user's profile Send private message

parry
MrExcel MVP


Joined: 21 Aug 2002
Posts: 1419
Location: Wellington, New Zealand
Flag: Newzealand

Status: Offline

 Reply with quote  

Re: Check for file before executing renainder of marco

Hi Trag, I can see you have a syntax error on the message box line. You only include brackets if your assigning the result of the message box to a variable.

You can just enter like this without the brackets...
MsgBox "NO FILE EXISTS"
You dont need vbokonly as thats the default button that appears.

An example of using the brackets is here where I want to know whether you pressed yes or no to determine which message to show.

Sub test()
Dim Answer
Answer = MsgBox("Are you happy?", vbYesNo)

If Answer = vbYes Then
MsgBox "Good for you!"
Else
MsgBox "Cheer up"
End If

End Sub

regards
parry

Post Thu Mar 25, 2004 8:43 am 
 View user's profile Send private message

BrianB
Board Master


Joined: 18 Feb 2003
Posts: 590
Location: Luton, England.
Flag: Uk

Status: Offline

 Reply with quote  

Re: Check for file before executing renainder of marco

Your code looks OK to me. Perhaps you have mis-spelled the path ?
Try checking what is actually returned by the function. Something like :-

code:
x=Dir("C:\Documents and Settings\Trag\Desktop\Test.xls")
Msgbox(x)



_________________
Regards
BrianB
Most problems occur from starting at the wrong place.
Use a cup of coffee to speed up all Windows processes
-------------------------------------------------

Post Thu Mar 25, 2004 8:46 am 
 View user's profile Send private message

Trag
Board Regular


Joined: 18 Dec 2003
Posts: 46
Location: Vancouver
Flag: Canada

Status: Offline

 Reply with quote  

Re: Check for file before executing renainder of marco

Parry was right, I had this part wrong...

MsgBox(vbOKOnly, "NO FILE EXISTS")

it should have been

MsgBox "NO FILE EXISTS"

It works just fine now.

Thanks for the help guys! icon_beerchug.gif

Post Thu Mar 25, 2004 8:53 am 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.