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

Hi, I need to read a text file and load it into excel. I
know the size of each field. I used the wizard, but I have
already done this 43 times with no end in sight. Is there
somebody out there that could give me a sample macro to
read each line of a text file, and split it up into
different fields? Thanks for the help. I would do it
myself, but I have no idea how to do it.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Loading text file

Private Sub CommandButton1_Click()
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

FName = Application.GetOpenFilename
Sep = InputBox("Enter the delimiter, for tab delimited data type tab")
If Sep = "tab" Then
Sep = vbTab
End If

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) < Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos = 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub

Dan E

"Clayton L. Wilson" wrote in message
...
Hi, I need to read a text file and load it into excel. I
know the size of each field. I used the wizard, but I have
already done this 43 times with no end in sight. Is there
somebody out there that could give me a sample macro to
read each line of a text file, and split it up into
different fields? Thanks for the help. I would do it
myself, but I have no idea how to do it.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Loading text file

oops,

First Line Should be
Public Sub ImportText()

Dan E

"Dan E" wrote in message
...
Private Sub CommandButton1_Click()
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

FName = Application.GetOpenFilename
Sep = InputBox("Enter the delimiter, for tab delimited data type tab")
If Sep = "tab" Then
Sep = vbTab
End If

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) < Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos = 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub

Dan E

"Clayton L. Wilson" wrote in

message
...
Hi, I need to read a text file and load it into excel. I
know the size of each field. I used the wizard, but I have
already done this 43 times with no end in sight. Is there
somebody out there that could give me a sample macro to
read each line of a text file, and split it up into
different fields? Thanks for the help. I would do it
myself, but I have no idea how to do it.





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Loading text file

Thanks for the macro, I'll try to figure it out, however
the text files are space delimited (formated text). Is
there any way to split the line up by column widths? Each
field is a different width as well.

-----Original Message-----
Private Sub CommandButton1_Click()
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

FName = Application.GetOpenFilename
Sep = InputBox("Enter the delimiter, for tab delimited

data type tab")
If Sep = "tab" Then
Sep = vbTab
End If

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) < Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos = 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub

Dan E

"Clayton L. Wilson"

wrote in message
...
Hi, I need to read a text file and load it into excel. I
know the size of each field. I used the wizard, but I

have
already done this 43 times with no end in sight. Is

there
somebody out there that could give me a sample macro to
read each line of a text file, and split it up into
different fields? Thanks for the help. I would do it
myself, but I have no idea how to do it.



.

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
Loading File in Excel 2007 Robert Bradley Excel Discussion (Misc queries) 1 April 1st 08 01:44 PM
How to create a graph after loading CSV file Leeky Charts and Charting in Excel 1 October 28th 07 01:44 AM
Click on file, Loading Excel problem Martin K. Excel Discussion (Misc queries) 2 June 3rd 07 07:54 PM
excel 2007 file loading error mallahd Excel Discussion (Misc queries) 1 March 21st 07 07:52 PM
when loading csv file to Excel, cells get converted to date format RB Excel Discussion (Misc queries) 3 February 14th 07 07:54 AM


All times are GMT +1. The time now is 09:59 AM.

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"