473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

importing flat text files

8
I have many text files to import into EXCEL. They are flat files and the data I need is in columns 1-3, 10-12 and 365-371. The files also have header records that jumble up the columns 1-3, 4-12 and 12-25.

I also need a way to load the files as part of the MACRO so the users will have less problems.
Nov 7 '07 #1
12 1715
Can you post a small sample of the file?

In the example can you denote where the header is jumbling the column.
Nov 7 '07 #2
this might work take itto access and then in to exscel this will only work if
that should put it in to excell
Nov 7 '07 #3
Killer42
8,435 Expert 8TB
Another option would be to have your VB/VBA code read the text file and write it back out in CSV format, then import it to Excel. On the other hand, if you're going to go to that much effort, I suppose you could just have your code read the text file and place the data into the worksheet.
Nov 8 '07 #4
boots
8
Can you post a small sample of the file?

In the example can you denote where the header is jumbling the column.
The header doesn't exactly jumble the column. The fields do not line up with the rest of the document.

header record:
HDRSH0590460106122720061227PROD2007010410053803

I need data from 1-3, 4-8, 9-19 in the header records. Some files have multiple header records.

I need data from 1-3, 10-12, 365-371.
this is a record that I need to extract data from

DET0000001739THIS NDC IS FOR A DRUG THAT IS USUALLY COVERED UNDER PART B. IF PLAN DETERMINES THAT THIS DRUG IS PART B COVERED, SUBMIT DELETION RECORD. 0000002100{
Nov 8 '07 #5
boots
8
Another option would be to have your VB/VBA code read the text file and write it back out in CSV format, then import it to Excel. On the other hand, if you're going to go to that much effort, I suppose you could just have your code read the text file and place the data into the worksheet.

Could this be done within the parameters that I described? I have a massive amount of files and any automation that gets me what I want would be worth it.
Nov 8 '07 #6
The fields do not line up with the rest of the document.
I'm not sure what you mean by that?

Are all the records the same length? Do the have and delimiters?
Nov 8 '07 #7
Killer42
8,435 Expert 8TB
How about something along these lines...

Expand|Select|Wrap|Line Numbers
  1. Private Sub ConvertFile()
  2.   Dim Text As String, Field(1 To 3) As String
  3.   Open "OldFile.Txt" For Input Access Read Shared As #1
  4.   ' Open "NewFile.Txt" For Output Access Write Lock Write As #2
  5.   Do Until EOF(1)
  6.     Line Input #1, Text
  7.     SplitText Text, Field()
  8.     Print #2, Field(1); vbTab; Field(2); vbTab; Field(3)
  9.   Loop
  10.   Close
  11. End Sub
  12.  
  13. Private Sub SplitText(ByVal Src As String, Dest() As String)
  14.  
  15.   If Left$(Src, 3) = "HDR" Then
  16.     Dest(1) = Left$(Src, 3)
  17.     Dest(2) = Mid$(Src, 4, 5)
  18.     Dest(3) = Mid$(Src, 365, 7)
  19.   Else
  20.     ' Populate Dest() array for detail record here.
  21.   End If
  22.  
  23. End Sub
This is just the guts of the routine, to give you some ideas. You will need to fill some gaps (like the Else clause in the SplitText routine) and work out exactly how you will make use of it. For instance, this will read the data file and write out the three fields separated by tabs. You might want to write it in CSV format instead, or load the info directly into your worksheet instead of writing it to a second file, or all sorts of variations.
Nov 8 '07 #8
boots
8
I'm not sure what you mean by that?

Are all the records the same length? Do the have and delimiters?

All the records are the same length and there are no delimiters.
Nov 9 '07 #9
Killer's example should work for you. With a minor tweak to shoot it into Excel.
Expand|Select|Wrap|Line Numbers
  1. Private Sub ConvertFile()
  2.   Dim Text As String, Field(1 To 3) As String
  3.   Open "OldFile.Txt" For Input Access Read Shared As #1
  4.   Do Until EOF(1)
  5.     Line Input #1, Text
  6.     SplitText Text, Field()
  7.     ActiveCell.Value = Field(1)
  8.     ActiveCell.Offset(,1).Activate 'move the active cell over one column
  9.     ActiveCell.Value = Field(2)
  10.     ActiveCell.Offset(,1).Activate
  11.     ActiveCell.Value = Field(3)
  12.     ActiveCell.Offset(1,-3).Activate 'move the active cell down one row and back 3 columns
  13.   Loop
  14.   Close
  15. End Sub
  16.  
  17. Private Sub SplitText(ByVal Src As String, Dest() As String)
  18.  
  19.   If Left$(Src, 3) = "HDR" Then
  20.     Dest(1) = Left$(Src, 3)
  21.     Dest(2) = Mid$(Src, 4, 5)
  22.     Dest(3) = Mid$(Src, 365, 7)
  23.   Else
  24.     ' Populate Dest() array for detail record here.
  25.   End If
  26.  
  27. End Sub
That will throw the results into Excel starting with the first active cell.

So just replace "OldFile.Txt" with the path and file name of your file and you should be good to go.
Nov 12 '07 #10
boots
8
thanks, that works very well!!
Nov 12 '07 #11
Killer42
8,435 Expert 8TB
thanks, that works very well!!
Excellent! :)

Nice teamwork, people.

Echooff3, hope you don't mind - I've just made a small correction in your code (and in mine that it was copied from). Changed all Field() references in the SplitText routine to Dest().
Nov 13 '07 #12
Rock on! Good eye Killer!
Nov 15 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: raykyoto | last post by:
Hi all, I'm sure this is a popular question that comes up every few months here. Indeed, I've looked at some of the past postings, but I would like to ask things differently. Basically, I'm...
1
by: D Mat | last post by:
Hi, I'm trying to get MS Access 2000 to automatically import a series of (~200) flat text, tab delimited, data files into a single Access table, with consistent fields and rows. The files have...
4
by: Ben | last post by:
So, at my place of employment, we use a national standard to transmit data between certain applications. This standard consists of a fixed width, flat file 4500-some-odd chars wide that contain...
12
by: JMO | last post by:
I can import a csv file with no problem. I can also add columns to the datagrid upon import. I want to be able to start importing at the 3rd row. This will pick up the headers necessary for the...
0
by: fboweb.com | last post by:
Hey there! I have a DTS import package from an old SQL server that takes flat text files, does some munging on the columns using ActiveX scripts and imports those flat files to various tables. ...
0
by: Silly Milly | last post by:
Calling ALL SSIS GURUS I am trying to import identical MDBs (Access Database) columns into SQL Server 2005 using foreach loop in SSIS. The data and file name of the MDBs are not the same; however...
1
by: olgolg | last post by:
Looking for a little assistance/direction. I have flat files that contain a header record, multiple detail records and a trailer record and sometimes more than one set of these in each flat file. ...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: raka61 | last post by:
Hi , I need help in understanding if i can directly(there are 100s of different structured files i get in text format with & hence dont wnat to manually define the structure for each file...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.