MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 13th, 2002, 02:02 PM   #1
Rick M
New Member
 
Join Date: Feb 2002
Location: Tampa, Fla
Posts: 44
Default

I have code the opens an existing filename in another directory. What I need is the code if the file is not found to pop up a msgbox stating "file not found" and exit the sub.
my statement looks like:
workbooks.open filename:=_
"c:documents and settingsdesktopforecastdallas.xls"

Thanks
Rick M is offline   Reply With Quote
Old May 13th, 2002, 02:20 PM   #2
alexxl
New Member
 
Join Date: May 2002
Posts: 4
Default

Search for "FileExists" in VBA help. Write an IF-THEN structure depending on what is returned. Use your existing codes if it is TRUE and use - MsgBox "File not found!" if it is FALSE.
alexxl is offline   Reply With Quote
Old May 13th, 2002, 02:27 PM   #3
bergy
Board Regular
 
Join Date: Apr 2002
Posts: 112
Default

Quote:
On 2002-05-13 13:20, alexxl wrote:
Search for "FileExists" in VBA help. Write an IF-THEN structure depending on what is returned. Use your existing codes if it is TRUE and use - MsgBox "File not found!" if it is FALSE.
alexxl is right. Here is something I have used before (with assistance from the Help File):

Sub test2()
Set fs = Application.FileSearch
With fs
.LookIn = "c:documents and settingsdesktopforecast"
.FileName = "dallas.xls"
If .Execute > 0 Then
Workbooks.Open FileName:= _
"c:\documents and settings\desktop\forecast\dallas.xls"
Else
MsgBox "File not found."
End If
End With
End Sub
bergy is offline   Reply With Quote
Old May 13th, 2002, 02:44 PM   #4
Rick M
New Member
 
Join Date: Feb 2002
Location: Tampa, Fla
Posts: 44
Default

Thanks, but I still cannot get it to work. It always says" file not found", when I know it is in the correct path
Rick M is offline   Reply With Quote
Old May 13th, 2002, 03:32 PM   #5
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-05-13 13:44, Rick M wrote:
Thanks, but I still cannot get it to work. It always says" file not found", when I know it is in the correct path
Hi Rick
What code did you use and have you typed
the names correctly ?
You could put a break in your code or use the
debug window to check what variables you are
getting.....
__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 14th, 2002, 01:38 PM   #6
Rick M
New Member
 
Join Date: Feb 2002
Location: Tampa, Fla
Posts: 44
Default

Ivan,
Here is the code:

Sub importcharlotte()
'
' importcharlotte Macro
' Macro recorded 5/13/2002 by Rick Moorman
'

'
Set fs = Application.FileSearch
With fs
.LookIn = "C:Documents and SettingsmoormanDesktopFORECAST"
.Filename = "charlotte.xls"
If .Execute > 0 Then
MsgBox "File not Found"
Else


Workbooks.Open "C:Documents and SettingsmoormanDesktopcharlotte.xls"
Range("A1:H37").Select
Selection.Copy
Windows("Sales Forecast -Actual.XLS").Activate
Sheets("Forecast").Select
ActiveWindow.SmallScroll Down:=177
Range("A215").Select
ActiveSheet.Paste
ActiveWindow.SmallScroll Down:=-15
Sheets("home").Select
Windows("charlotte.xls").Activate
ActiveWindow.Close


End If
End With




End Sub
Rick M is offline   Reply With Quote
Old May 14th, 2002, 04:48 PM   #7
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Hi Ricky
Try;


Sub importcharlotte()
'
' importcharlotte Macro
' Macro recorded 5/13/2002 by Rick Moorman
'

'
Set fs = Application.FileSearch
With fs
.LookIn = "C:Documents and SettingsmoormanDesktopFORECAST"
.Filename = "charlotte.xls"
If .Execute = 0 Then
MsgBox "File not Found"
Else
Workbooks.Open "C:Documents and SettingsmoormanDesktopcharlotte.xls"
Range("A1:H37").Select
Selection.Copy
Windows("Sales Forecast -Actual.XLS").Activate
Sheets("Forecast").Select
Range("A215").Select
ActiveSheet.Paste
Sheets("home").Select
Windows("charlotte.xls").Activate
ActiveWindow.Close
End If
End With

End Sub


__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 15th, 2002, 06:47 AM   #8
Rick M
New Member
 
Join Date: Feb 2002
Location: Tampa, Fla
Posts: 44
Default

Still trying:
It works as long as the file exist. When the file does not exist, it errors right after the .execute statement

Workbooks.Open "C:Documents and SettingsmoormanDesktopforecastcharlotte.xls"

like it believes the If .execute = 0 then

Thanks for your help. it is making me crazy.
Rick M is offline   Reply With Quote
Old May 15th, 2002, 09:27 AM   #9
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-05-15 05:47, Rick M wrote:
Still trying:
It works as long as the file exist. When the file does not exist, it errors right after the .execute statement

Workbooks.Open "C:Documents and SettingsmoormanDesktopforecastcharlotte.xls"

like it believes the If .execute = 0 then

Thanks for your help. it is making me crazy.
Not sure why it wouldn't work....worked on
my system ??

Try adding this line >

.NewSearch

eg.
With fs
.NewSearch
.LookIn = "C:Documents and SettingsmoormanDesktopFORECAST"


__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 20th, 2002, 09:36 AM   #10
Rick M
New Member
 
Join Date: Feb 2002
Location: Tampa, Fla
Posts: 44
Default

Thanks Ivan - fixed it using

Dim Path As String
Dim FileName As String

Path = "C:documents and SettingsmoormanDesktopFORECAST"
FileName = "charlotte.xls"

If UCase(FileName) = UCase(Dir(Path & FileName)) Then
Workbooks.Open Path & FileName


It works - that's all I know
Rick M is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 12:18 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes