Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Import txt file with fixed length from VBA

Hi guys,

If anyone could give me some pointers on how to go about this I'd appreciate
it very much.

situation:
I have to import various textfiles using VBA. I select the filename and path
which comes from a listview and a textbox on a userform. So far so good.

The textfile however is setup as follows:

xxx xxxxx xxxx xxxxx xxxxx

The data has fixed length for example the first field is 10chars, the second
20chars, the third 8 chars etc. (got about 150 rows of data) every row has
the same fieldspecs.

How would I go about it to import this data into a designated sheet from VBA?
Using the wizard will open the file and place it on a new workbook and
that's not what I want..

Thanks in advance for any pointers.
--
Maurice
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Import txt file with fixed length from VBA

You could use Data|Import External Data|Import Data (xl2003 menus).

And at the last (or close to the last) dialog of that wizard, you'll be asked
where the data should go.

Personally, I'd just use exactly what you used, but add a couple of more steps.

One step to copy the data to the location where I wanted it and the second to
close without saving the text file that I just opened.

Option Explicit
Sub Testme()

Dim DestCell As Range
Dim TxtWks As Worksheet

'assign the destination cell -- I have no idea what you want.
Set DestCell = Worksheets("Sheet1").Range("x99")

'your code to open the file
Workbooks.OpenText Filename:=....

Set TxtWks = ActiveSheet

With TxtWks
.UsedRange.Copy _
Destination:=DestCell
.Parent.Close savechanges:=False
End With

End Sub




Maurice wrote:

Hi guys,

If anyone could give me some pointers on how to go about this I'd appreciate
it very much.

situation:
I have to import various textfiles using VBA. I select the filename and path
which comes from a listview and a textbox on a userform. So far so good.

The textfile however is setup as follows:

xxx xxxxx xxxx xxxxx xxxxx

The data has fixed length for example the first field is 10chars, the second
20chars, the third 8 chars etc. (got about 150 rows of data) every row has
the same fieldspecs.

How would I go about it to import this data into a designated sheet from VBA?
Using the wizard will open the file and place it on a new workbook and
that's not what I want..

Thanks in advance for any pointers.
--
Maurice


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Import txt file with fixed length from VBA

Here some sites to learn about this:
http://www.trixar.com/~makai/minidb1.htm
http://msdn.microsoft.com/en-us/libr...71(VS.60).aspx
http://babek.info/libertybasicfiles/.../nl120/raf.htm
http://www.vb-helper.com/howto_random_access.html
http://support.microsoft.com/kb/150700
http://www.profsr.com/vb/vbless08.htm

RBS


"Maurice" wrote in message
...
Hi guys,

If anyone could give me some pointers on how to go about this I'd
appreciate
it very much.

situation:
I have to import various textfiles using VBA. I select the filename and
path
which comes from a listview and a textbox on a userform. So far so good.

The textfile however is setup as follows:

xxx xxxxx xxxx xxxxx xxxxx

The data has fixed length for example the first field is 10chars, the
second
20chars, the third 8 chars etc. (got about 150 rows of data) every row has
the same fieldspecs.

How would I go about it to import this data into a designated sheet from
VBA?
Using the wizard will open the file and place it on a new workbook and
that's not what I want..

Thanks in advance for any pointers.
--
Maurice


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Import txt file with fixed length from VBA

Dave,

It's those last steps i should take into consideration. Thanks for replying
;-)
--
Maurice Ausum


"Dave Peterson" wrote:

You could use Data|Import External Data|Import Data (xl2003 menus).

And at the last (or close to the last) dialog of that wizard, you'll be asked
where the data should go.

Personally, I'd just use exactly what you used, but add a couple of more steps.

One step to copy the data to the location where I wanted it and the second to
close without saving the text file that I just opened.

Option Explicit
Sub Testme()

Dim DestCell As Range
Dim TxtWks As Worksheet

'assign the destination cell -- I have no idea what you want.
Set DestCell = Worksheets("Sheet1").Range("x99")

'your code to open the file
Workbooks.OpenText Filename:=....

Set TxtWks = ActiveSheet

With TxtWks
.UsedRange.Copy _
Destination:=DestCell
.Parent.Close savechanges:=False
End With

End Sub




Maurice wrote:

Hi guys,

If anyone could give me some pointers on how to go about this I'd appreciate
it very much.

situation:
I have to import various textfiles using VBA. I select the filename and path
which comes from a listview and a textbox on a userform. So far so good.

The textfile however is setup as follows:

xxx xxxxx xxxx xxxxx xxxxx

The data has fixed length for example the first field is 10chars, the second
20chars, the third 8 chars etc. (got about 150 rows of data) every row has
the same fieldspecs.

How would I go about it to import this data into a designated sheet from VBA?
Using the wizard will open the file and place it on a new workbook and
that's not what I want..

Thanks in advance for any pointers.
--
Maurice


--

Dave Peterson

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
fixed length file Steve S[_2_] Excel Discussion (Misc queries) 2 March 13th 07 04:12 PM
fixed length file to excel? VBA macro possible? [email protected] Excel Programming 16 October 6th 06 07:33 PM
Fixed length text file Eric Excel Discussion (Misc queries) 1 July 12th 06 10:02 PM
Fixed length file import?? Trey[_2_] Excel Programming 3 February 9th 06 08:44 PM
2 Qs: grab net page, and import as fixed length format Keith R[_3_] Excel Programming 2 December 12th 03 08:18 PM


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