Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 192
Default Extract data question

Hello,

I am completely new to VBA so I hope you will bear with me...
My problem is that I need to extract certain data is always in the same
format, ie

[STATION NUMBER]
12005
[END]
[CDS Details]
NAME,MUICK
LOCATION,INVERMUICK
NOMINAL AREA, 110.00
NOMINAL NGR,336400,794700
[End]

where I need to extract the 2nd and 8th lines, and place them in adjacent
colums in an excel file, then go to the next file down (named numerically)

Many many thanks for any help - I have about 1000 files to go through!

Nick
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Extract data question

If the files were file1.txt to file1000.txt found in the C:\ directory:

Sub ABC()
Dim sh As Worksheet
Dim bk As Workbook
Dim i As Long
Set sh = ActiveSheet
For i = 1 To 1000
Set bk = Workbooks.Open("c:\file" & i & ".txt")
sh.Cells(i, 1) = bk.Worksheets(1).Cells(2, 1)
sh.Cells(i, 2) = bk.Worksheets(1).Cells(8, 1)
bk.Close Savechanges:=False
Next
End Sub

--
Regards,
Tom Ogilvy


"Nick" wrote in message
...
Hello,

I am completely new to VBA so I hope you will bear with me...
My problem is that I need to extract certain data is always in the same
format, ie

[STATION NUMBER]
12005
[END]
[CDS Details]
NAME,MUICK
LOCATION,INVERMUICK
NOMINAL AREA, 110.00
NOMINAL NGR,336400,794700
[End]

where I need to extract the 2nd and 8th lines, and place them in adjacent
colums in an excel file, then go to the next file down (named numerically)

Many many thanks for any help - I have about 1000 files to go through!

Nick



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 230
Default Extract data question

I would put them all in a folder and run the code below.

Note: I have assumed you will name the folder MyFiles and store it on C
Only the text files should be in that folder

Private Sub LoadValuesFromTextFiles()
Dim FSO, FLD, FIL
Dim N As Long, i As Long
Dim S As String
Dim LineCount As Long

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLD = FSO.GetFolder("C:\MyFiles")

'Assign the next free file to N
N = FreeFile()

Application.ScreenUpdating = False
For Each FIL In FLD.Files
'Open Text File For Input
Open FIL.Path For Input As #N
'Loop Until the either End Of File or row 9
While Not EOF(N) And LineCount < 8
LineCount = LineCount + 1
Line Input #N, S
If LineCount = 2 Or LineCount = 8 Then
If Left(S, 1) = "=" Then S = "'" & S
ActiveCell.Offset(i, 0).Value = S
i = i + 1
End If
Wend 'Not EOF(N) And LineCount < 8
LineCount = 0
Close #N
Next FIL
Application.ScreenUpdating = True

End Sub

"Nick" wrote in message
...
Hello,

I am completely new to VBA so I hope you will bear with me...
My problem is that I need to extract certain data is always in the same
format, ie

[STATION NUMBER]
12005
[END]
[CDS Details]
NAME,MUICK
LOCATION,INVERMUICK
NOMINAL AREA, 110.00
NOMINAL NGR,336400,794700
[End]

where I need to extract the 2nd and 8th lines, and place them in adjacent
colums in an excel file, then go to the next file down (named numerically)

Many many thanks for any help - I have about 1000 files to go through!

Nick



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
Need help in extract of data Ms. Latte Excel Discussion (Misc queries) 2 August 8th 08 05:27 AM
Tying to extract all data from a pivot pulling from external data Ted Urban Excel Discussion (Misc queries) 3 September 14th 07 10:50 AM
How to extract the data Shiva Excel Worksheet Functions 2 November 1st 05 04:41 AM
extract data miles Excel Discussion (Misc queries) 1 October 19th 05 11:37 AM
extract data John Collins[_4_] Excel Programming 5 December 9th 03 06:18 PM


All times are GMT +1. The time now is 09:31 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"