Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default vba code in Excel

I am trying to create a vba code in Excel to go to a list of files and
compare the file name that I entered manually with the list to determine if
there is a file out there by that name. If there is not a file out there, an
error message will be generated telling me that the file does not exist and
to enter a new file name.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default vba code in Excel

sName = "MyFile.xls"
if dir("C:\MyFolder\" & sName) = "" then
msgbox sName & " does not exist, enter another name"
exit sub
end if


or instead, use

sname = Application.GetOpenFileName()

--
Regards,
Tom Ogilvy


"ExcelNoviceInIL" wrote in
message ...
I am trying to create a vba code in Excel to go to a list of files and
compare the file name that I entered manually with the list to determine

if
there is a file out there by that name. If there is not a file out there,

an
error message will be generated telling me that the file does not exist

and
to enter a new file name.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default vba code in Excel

Thank you for the help. It worked perfectly!

"Tom Ogilvy" wrote:

sName = "MyFile.xls"
if dir("C:\MyFolder\" & sName) = "" then
msgbox sName & " does not exist, enter another name"
exit sub
end if


or instead, use

sname = Application.GetOpenFileName()

--
Regards,
Tom Ogilvy


"ExcelNoviceInIL" wrote in
message ...
I am trying to create a vba code in Excel to go to a list of files and
compare the file name that I entered manually with the list to determine

if
there is a file out there by that name. If there is not a file out there,

an
error message will be generated telling me that the file does not exist

and
to enter a new file name.




  #4   Report Post  
Posted to microsoft.public.excel.programming
jai jai is offline
external usenet poster
 
Posts: 71
Default vba code in Excel

Just wanted to add a little more to this. The response to the
dir(Pathname + filename) is the file name if it exists in the given path.
That is if myworkbook.xls is in C:\Directory, then
filename = dir(C:\Directory\myworkbook.xls) will return myworkbook.xls
To open files without getting all those warnings for already open files and
files not existing, I use a small utility that checks and returns the file
handle as given below

dim mywb as workbook

set mywb = OpenFile(PathName, FileName, FilePassword)

function OpenFile( Pathname as String, Filename as String, Password as
String) as Workbook
- checks whether the workbook is already open. I call a function IsOpen
that checks for the workbook in the workbooks collection. If it exists, it
returns a workbook handle which Openfile returns. In case the wb is not open,
use the directory command to check if the file exists. On getting the
required string as response, use the workbooks.Open method to open it and
return the workbook handle.

Perhaps I have complicated things. Any suggestions for improvement and
increased efficiency are most welcome.

Jai


"ExcelNoviceInIL" wrote:

Thank you for the help. It worked perfectly!

"Tom Ogilvy" wrote:

sName = "MyFile.xls"
if dir("C:\MyFolder\" & sName) = "" then
msgbox sName & " does not exist, enter another name"
exit sub
end if


or instead, use

sname = Application.GetOpenFileName()

--
Regards,
Tom Ogilvy


"ExcelNoviceInIL" wrote in
message ...
I am trying to create a vba code in Excel to go to a list of files and
compare the file name that I entered manually with the list to determine

if
there is a file out there by that name. If there is not a file out there,

an
error message will be generated telling me that the file does not exist

and
to enter a new file name.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import VBA Code in Excel-File ? (Export VBA Code to file) Matthias Pospiech Excel Programming 2 March 22nd 05 04:56 PM
stubborn Excel crash when editing code with code, one solution Brian Murphy Excel Programming 0 February 20th 05 05:56 AM
Word VBA Code to Excel Code Lee Excel Programming 1 September 24th 04 04:03 AM
Excel XP VBA code to search all macro code in Excel module for specific search string criteria Ed[_18_] Excel Programming 4 May 20th 04 02:08 PM
Excel XP VBA code to search all macro code in Excel module for specific search string criteria Frank Kabel Excel Programming 0 May 19th 04 08:11 PM


All times are GMT +1. The time now is 12:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"