PC Review


Reply
Thread Tools Rate Thread

Changing file name for a data import from a text file

 
 
Riddler
Guest
Posts: n/a
 
      4th Oct 2007
I have a text file that I am settup up to have imported into a sheet
and then do some calculations with that data. That all works fine. The
issue I am having is writing the macro to import the data again to the
same location and everything but the name of the text file has
changed. I am pulling data from another computer that creates a text
file for every day. It has a name like 20071003.s and 20071002.s
Here is the code I recorded and can rerun to refresh the data from the
same file;

Sub Macro1()
With ActiveSheet.QueryTables.Add(Connection:="TEXT;\\ULTRAVISION
\Opti information\prodhist\200710\20071002.s", Destination _
:=Range("D5"))
.Name = "20071002.s"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub


Here is what I tried to chage it to, to change where the data comes
from 20071003.s instead of 20071002.s
I am on the same sheet when I run this also so that is not a issue.
The file exists and I can import it manually.
THe error I get is a runtime error 1004 that says "Excel cannot find
the text file to refresh this external data range"

Sub Macro1()
Dim NewDataFileNameandPath As String
Dim NewDataFileName As String

NewDataFileName = "20071003.s_1"
NewDataFileNameandPath = "\\ULTRAVISION\Opti information\prodhist
\200710\20071003.s"

With
ActiveSheet.QueryTables.Add(Connection:="TEXT;NewDataFileNameandPath",
Destination _
:=Range("D5"))
.Name = NewDataFileName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub

Thanks
Scott

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      4th Oct 2007
The error will occur if the NewDataFileNameandPath doesn't exist. Make sure
the filename includes .s_1. The NewDataFileName (withoutpath) doesn't have
to inlcude the _1.


NewDataFileName = "20071003.s_1"
NewDataFileNameandPath = "\\ULTRAVISION\Opti information\prodhist
\200710\20071003.s"


"Riddler" wrote:

> I have a text file that I am settup up to have imported into a sheet
> and then do some calculations with that data. That all works fine. The
> issue I am having is writing the macro to import the data again to the
> same location and everything but the name of the text file has
> changed. I am pulling data from another computer that creates a text
> file for every day. It has a name like 20071003.s and 20071002.s
> Here is the code I recorded and can rerun to refresh the data from the
> same file;
>
> Sub Macro1()
> With ActiveSheet.QueryTables.Add(Connection:="TEXT;\\ULTRAVISION
> \Opti information\prodhist\200710\20071002.s", Destination _
> :=Range("D5"))
> .Name = "20071002.s"
> .FieldNames = True
> .RowNumbers = False
> .FillAdjacentFormulas = False
> .PreserveFormatting = True
> .RefreshOnFileOpen = False
> .RefreshStyle = xlInsertDeleteCells
> .SavePassword = False
> .SaveData = True
> .AdjustColumnWidth = True
> .RefreshPeriod = 0
> .TextFilePromptOnRefresh = False
> .TextFilePlatform = 437
> .TextFileStartRow = 1
> .TextFileParseType = xlDelimited
> .TextFileTextQualifier = xlTextQualifierDoubleQuote
> .TextFileConsecutiveDelimiter = True
> .TextFileTabDelimiter = True
> .TextFileSemicolonDelimiter = False
> .TextFileCommaDelimiter = False
> .TextFileSpaceDelimiter = True
> .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
> 1, 1, 1)
> .TextFileTrailingMinusNumbers = True
> .Refresh BackgroundQuery:=False
> End With
> End Sub
>
>
> Here is what I tried to chage it to, to change where the data comes
> from 20071003.s instead of 20071002.s
> I am on the same sheet when I run this also so that is not a issue.
> The file exists and I can import it manually.
> THe error I get is a runtime error 1004 that says "Excel cannot find
> the text file to refresh this external data range"
>
> Sub Macro1()
> Dim NewDataFileNameandPath As String
> Dim NewDataFileName As String
>
> NewDataFileName = "20071003.s_1"
> NewDataFileNameandPath = "\\ULTRAVISION\Opti information\prodhist
> \200710\20071003.s"
>
> With
> ActiveSheet.QueryTables.Add(Connection:="TEXT;NewDataFileNameandPath",
> Destination _
> :=Range("D5"))
> .Name = NewDataFileName
> .FieldNames = True
> .RowNumbers = False
> .FillAdjacentFormulas = False
> .PreserveFormatting = True
> .RefreshOnFileOpen = False
> .RefreshStyle = xlInsertDeleteCells
> .SavePassword = False
> .SaveData = True
> .AdjustColumnWidth = True
> .RefreshPeriod = 0
> .TextFilePromptOnRefresh = False
> .TextFilePlatform = 437
> .TextFileStartRow = 1
> .TextFileParseType = xlDelimited
> .TextFileTextQualifier = xlTextQualifierDoubleQuote
> .TextFileConsecutiveDelimiter = True
> .TextFileTabDelimiter = True
> .TextFileSemicolonDelimiter = False
> .TextFileCommaDelimiter = False
> .TextFileSpaceDelimiter = True
> .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
> 1, 1, 1)
> .TextFileTrailingMinusNumbers = True
> .Refresh BackgroundQuery:=False
> End With
> End Sub
>
> Thanks
> Scott
>
>

 
Reply With Quote
 
Riddler
Guest
Posts: n/a
 
      4th Oct 2007
That was just a typo. Both should have the "_1". It still doesnt work
with it though.

Scott



 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      4th Oct 2007
Lets make sure excel can really find the file. try the code below. Dir
returns a empty string if the file is not found.

I think the problem is your network address starting with the double
backslash. Do you have the Network name mapped to a drive letter? You may
havve to go into a window explorer (not internet) and map you network dirve
(under tools menu). Then use drive letter.

sub test()

flnm = Dir("\\ULTRAVISION\Opti information\prodhist\200710\20071003.s")
if flnm = "" then
msgbox("File not found")
else
msg box("File Found : " & flnm)
end if

end sub

"Riddler" wrote:

> That was just a typo. Both should have the "_1". It still doesnt work
> with it though.
>
> Scott
>
>
>
>

 
Reply With Quote
 
Riddler
Guest
Posts: n/a
 
      4th Oct 2007
I got it figured out. I recorded the code when I edited the existing
query and it works now. Here is what I came up with.

Scott

Sub RefreshExistingQuery()
'This code will refresh a existing query with new data from a new file
DataFileName = "TEXT;\\ULTRAVISION\Opti information\prodhist
\200710\" & Sheets("Sheet1").Cells(3, 1).Text & ".s"
With Sheets("Sheet1").Range("D5").QueryTable
.Connection = DataFileName
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1)
.Refresh BackgroundQuery:=False
End With
End Sub

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get External Data, Import Text File, File name problem Scott Riddle Microsoft Excel Programming 3 5th Feb 2010 09:20 PM
Import data from Excel file or text file into database in ASP.NET program bienwell Microsoft ASP .NET 2 17th Jun 2006 04:57 AM
import external data from changing file name =?Utf-8?B?bmF0aGFu?= Microsoft Excel Worksheet Functions 2 6th Apr 2005 04:39 PM
How do I import text file, analyze data, export results, open next file Geoffro Microsoft Excel Programming 2 6th Mar 2005 08:02 PM
Open delimited text file to excel without changing data in that file zohanc Microsoft Excel Programming 1 3rd Oct 2003 01:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:27 PM.