#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 110
Default Test if file exists

From Excel, I would like to test if a txt file is in a certain folder.

For example, I would like the simple code to know if mytext.txt is in folder
L:\Storage\

Secondly, I have the line to open a text file to a worksheet

Workbooks.OpenText Filename:="L:\Storage\myText.xls"

But if I want that to be imported to the active worksheet, how do I do that?

Thanks in advance!

regards
Daniel Bonallack

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Test if file exists

Dim DestCell as range
dim TestStr as string

set destcell = activesheet.range("a2") 'whereever you want

teststr = ""
on error resume next
teststr = dir("L:\Storage\myText.txt")
on error goto 0

if teststr = "" then
msgbox "It's not there"
else
workbooks.opentext filename:="L:\Storage\myText.txt"
activesheet.usedrange.copy _
destination:=destcell
activeworkbook.close savechanges:=false
end if

============
You could also record a macro when you do Data|Import external data (xl2003
menus). You'll be able to specify where you want the data to go. But you'd
still need to incorporate the test into that recorded macro.

Daniel Bonallack wrote:

From Excel, I would like to test if a txt file is in a certain folder.

For example, I would like the simple code to know if mytext.txt is in folder
L:\Storage\

Secondly, I have the line to open a text file to a worksheet

Workbooks.OpenText Filename:="L:\Storage\myText.xls"

But if I want that to be imported to the active worksheet, how do I do that?

Thanks in advance!

regards
Daniel Bonallack


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 110
Default Test if file exists

Dave, thanks so much - perfect

"Dave Peterson" wrote:

Dim DestCell as range
dim TestStr as string

set destcell = activesheet.range("a2") 'whereever you want

teststr = ""
on error resume next
teststr = dir("L:\Storage\myText.txt")
on error goto 0

if teststr = "" then
msgbox "It's not there"
else
workbooks.opentext filename:="L:\Storage\myText.txt"
activesheet.usedrange.copy _
destination:=destcell
activeworkbook.close savechanges:=false
end if

============
You could also record a macro when you do Data|Import external data (xl2003
menus). You'll be able to specify where you want the data to go. But you'd
still need to incorporate the test into that recorded macro.

Daniel Bonallack wrote:

From Excel, I would like to test if a txt file is in a certain folder.

For example, I would like the simple code to know if mytext.txt is in folder
L:\Storage\

Secondly, I have the line to open a text file to a worksheet

Workbooks.OpenText Filename:="L:\Storage\myText.xls"

But if I want that to be imported to the active worksheet, how do I do that?

Thanks in advance!

regards
Daniel Bonallack


--

Dave Peterson

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
Check if file exists Jon Excel Discussion (Misc queries) 14 October 4th 07 04:57 PM
File Exists Formula El Bee Excel Worksheet Functions 4 November 9th 06 03:45 PM
Determine if a File Exists Connie Excel Discussion (Misc queries) 1 November 8th 06 09:11 AM
How to check if a file exists in an ftp folder LL Cool A Excel Discussion (Misc queries) 3 May 16th 06 09:22 PM
File Exists Mike McLellan Excel Discussion (Misc queries) 2 May 4th 06 09:20 AM


All times are GMT +1. The time now is 09:11 PM.

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

About Us

"It's about Microsoft Excel"