Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default import file programatically

Thanks.
I have managed to do it using the following code:

Sub ImportDat()
Dim strLine As String
Dim i As Long
Dim r As Long
Dim strJobNo As String
Dim ValidLine As Boolean
Dim lJob As Long

ActiveSheet.Select
i = 1
r = 1
Close
Open "C:\Jobs.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, strLine

If Mid(strLine, 46, 3) = "Job" Then
strJobNo = Mid(strLine, 50, 8)
GoTo lEnd
ElseIf Left(strLine, 1) = "0" Then
lJob = 2
ValidLine = True
ElseIf Left(strLine, 1) = "7" Then
lJob = 3
ValidLine = True
Else
lJob = 0
ValidLine = False

GoTo lEnd
End If

If ValidLine = True Then
If lJob = 2 Then
ActiveSheet.Cells(r, 1).Value = strJobNo
ActiveSheet.Cells(r, 2).Value = "'" & Left(strLine, 5)
ActiveSheet.Cells(r, 3).Value = Mid(strLine, 45, 16)
ElseIf lJob = 3 Then
ActiveSheet.Cells(r, 1).Value = strJobNo
ActiveSheet.Cells(r, 2).Value = "'" & Left(strLine, 5)
ActiveSheet.Cells(r, 3).Value = Mid(strLine, 106, 15)
End If
End If
r = r + 1
lEnd:
i = i + 1
Loop
Close #1
End Sub

"Philip" wrote in message
...
Hi,

well, what you can do is read it in directly into one column (using Text
Import - data Menu) then split it up from there using Filter and Find with
pattern matching...

we built a re-usable tool thet reads a text file of un-normalised
(unstructured) data (of 450,000 lines!) into a buffer, then uses regular
expressions and xml to normalise it before transforming it into csv and
using
ADO/ODBC to import it - but that may be a little too much work for what
you
need.

To read how to treat text file data as database tables, read this article
on
the MSDN about Schema Files ("Much ADO about Text Files" article):
http://msdn.microsoft.com/library/de...ng03092004.asp

I hope that gives you a start

Philip
"Newbie" wrote:

I have a text file that is not in a state to just import i.e. the data is
not in columns - the job no is centred on the document and there are
underlines that I don't want importing
eg

Postion 50 Line 1: Job No: 1234567
Postion 1 Line 2: Analysis Code (len 5)
Position 35 Line 2: Cost (99999999.99)
Position 63 Line 2: Sales (99999999.99)

The lines to be imported always start with a 0 or a 7 except for the Job
No.

How can I programmatically import this file so that it gives me 4 columns
Col 1 = Job No
Col 2 = Analysis Code
Col 3 = Cost or Sales


The job No needs to be repeated in col 1 on each line until the next job
no
is found.

Thanks

The file looks like this
Job No: 1234567
00123 99.99
00356 100.99
70000 1000.00

I would like the import to look like this:
1234567 00123 99.99
1234567 00356 100.99
1234567 70000 1000.00






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
programatically selecting columns to import text into ScottM Excel Discussion (Misc queries) 2 November 25th 05 05:41 PM
Programatically close a file without saving [email protected] Excel Programming 2 December 26th 04 01:23 AM
Creating a PDF file programatically from Excel Chaplain Doug Excel Discussion (Misc queries) 0 December 16th 04 08:49 PM
Programatically saving an excel .csv file to .xls file... Pai Excel Programming 1 December 2nd 03 05:18 AM
Need help on How to programatically Import VBA Project Excel Objects, Forms, Modules JMMach Excel Programming 7 October 21st 03 08:46 PM


All times are GMT +1. The time now is 02:57 PM.

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

About Us

"It's about Microsoft Excel"