Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Import VBA Code in Excel-File ? (Export VBA Code to file) | Excel Programming | |||
stubborn Excel crash when editing code with code, one solution | Excel Programming | |||
Word VBA Code to Excel Code | Excel Programming | |||
Excel XP VBA code to search all macro code in Excel module for specific search string criteria | Excel Programming | |||
Excel XP VBA code to search all macro code in Excel module for specific search string criteria | Excel Programming |