Impoer text file

Boris7265

Board Regular
Joined
Apr 6, 2011
Messages
68
Hi Everybody ,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
I have small problem and I hope somebody will be able to help me . I am not very knowledge in the vb so here is problem .
Every day I imported external data out of txt file into spreadsheet .I created macro and the code is below:
<o:p> </o:p>
"TEXT;Y:\BorisGomel\NAN\email attachment\CAPNAN-MAY2011\A1940660.TXT", _<o:p></o:p>
Destination:=Range("A1"))<o:p></o:p>
.Name = "A1940660"<o:p></o:p>
.FieldNames = True<o:p></o:p>
.RowNumbers = False<o:p></o:p>
.FillAdjacentFormulas = False<o:p></o:p>
.PreserveFormatting = True<o:p></o:p>
.RefreshOnFileOpen = False<o:p></o:p>
.RefreshStyle = xlInsertDeleteCells<o:p></o:p>
.SavePassword = False<o:p></o:p>
.SaveData = True<o:p></o:p>
.AdjustColumnWidth = True<o:p></o:p>
.RefreshPeriod = 0<o:p></o:p>
.TextFilePromptOnRefresh = False<o:p></o:p>
.TextFilePlatform = 437<o:p></o:p>
.TextFileStartRow = 1<o:p></o:p>
.TextFileParseType = xlFixedWidth<o:p></o:p>
.TextFileTextQualifier = xlTextQualifierDoubleQuote<o:p></o:p>
.TextFileConsecutiveDelimiter = False<o:p></o:p>
.TextFileTabDelimiter = True<o:p></o:p>
.TextFileSemicolonDelimiter = False<o:p></o:p>
.TextFileCommaDelimiter = False<o:p></o:p>
.TextFileSpaceDelimiter = False<o:p></o:p>
.TextFileColumnDataTypes = Array(1, 9, 1, 1, 1, 1, 1, 1, 1, 9)<o:p></o:p>
.TextFileFixedColumnWidths = Array(4, 1, 8, 20, 26, 16, 14, 13, 20)<o:p></o:p>
.TextFileTrailingMinusNumbers = True<o:p></o:p>
.Refresh BackgroundQuery:=False
<o:p> </o:p>
Everything is working fine , but there is another file txt every day so I have to change in Y:\BorisGomell\NAN\email attachment\CAPNAN-MAY2011\A1940660.TXT part A19406600 every day due to another name of text file and in the next month I have to change CAPNAN-MAY2011 on JULE2011.I do it in the code , but the there a few users that use this spreadsheet and they are really users , they can not change code . As I said before I have very superficially knowledge in VB Excel and I am really appreciated if somebody point me out how I can program message box that will ask what text file to import and once text file is provided it will change accordingly directly part of A1940660 and leave the same rest of it .
<o:p> </o:p>
Thank you,
<o:p> </o:p>
The Best Reagrds.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Here is a thought. You could prompt for the file name as well as the month.

Here is an example of prompting for the file name.

Range("a1:az5000").ClearContents

Dim file As String
'Dim loc As String
'Dim ext As String

'loc = "c:\"
'ext = ".txt"
file = "c:\" + InputBox("Enter File Name", "Enter File Name", "") + ".txt"

With ActiveSheet.QueryTables _
.Add(connection:="TEXT;" + file, Destination:=Range("A1"))
.Name = "A1940660"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 9, 1, 1, 1, 1, 1, 1, 1, 9)
.TextFileFixedColumnWidths = Array(4, 1, 8, 20, 26, 16, 14, 13, 20)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
 
Upvote 0
Thank you very much Dartagnar .

I have made a little bit modification of your code such as :

Dim Filename As String
Filename = InputBox("Enter the file name")



With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;Y:\BorisGomell\NAN\email attachment\CAPNAN-MAY2011\" + Filename + ".TXT",

and it is working beatufully .

Thank you again and really appreciate you help .

Just one small things if you are not so tired of me .I insert a new sheet every day to import the text file .Can I programm or puit this into vb code this as well .I have the following code:

Dim Filename As String
Filename = InputBox("Enter the file name")

Worksheets.Add().Name = Filename

It is working as well nicely , but I need the new sheet to be active that The import code can work on it and import the file onto new worksheet that just was added to the workbook .I am working on it but so far I have gotte no success.

Thnak you for advice.

The best Reagard.

BorisGomel
 
Upvote 0
I think the following code will do what you are looking for. A new worksheet is created with the file name used to name the worksheet.

Sub import()

Dim file As String
Dim location
file = InputBox("Enter File Name")
Worksheets.Add().Name = file
location = "c:\" + file + ".txt"

Sheets(file).Select

With ActiveSheet.QueryTables _
.Add(Connection:="TEXT;" + location, Destination:=Range("A1"))
.Name = "A1940660"
.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 = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 9, 1, 1, 1, 1, 1, 1, 1, 9)
.TextFileFixedColumnWidths = Array(4, 1, 8, 20, 26, 16, 14, 13, 20)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

End Sub
 
Upvote 0
Thank you very much Dartagnan!!!

Just Wow!!!

The code is working!!!

The Best Regards.

Happy Holidays

BorisGomel
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top