?
Solved

Excel-Importing a Tab delimited file usig VBA

Posted on 2003-03-18
6
Medium Priority
?
324 Views
Last Modified: 2007-12-19
I have a Tab delimited file and I want to bring it into my excel doc filling sheet1 with the data in the file.  I plan on putting the data into an array and then filling each cell with the array one line at a time. For example..I would put the data from line 1 into an array, then fill the first row of sheet1 with the values in the array, then clear the array and do the same for the second line and so on.  But I'm unsure of how to read the Tab delimited file into Excel.  Any help would be greatly appreciated.  Thank you in advance.

Sam  
0
Comment
Question by:Sam8932
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Expert Comment

by:itzme
ID: 8160380
The way I would start doing it would be to read the file one line at a time as input and the split that line up into a string array using the split command e.g

OPEN "filename" FOR INPUT as #1

WHILE NOT EOF(1)
LINE INPUT #1, line$

<array> = Split(line$, vbTab, -1, vbTextCompare)
For i% = 0 To UBound(<array>)
      <excelsheet>.Cells(<line>,i) = <array>(i)
Next i%

WEND
CLOSE #1

This is a bit rough but could do, you'll also have to think about the way split returns to different indexes, i.e if there's something in index 0 it means that there was no splitting.


0
 
LVL 1

Accepted Solution

by:
Ysgrathe earned 980 total points
ID: 8160422
I would use the newer file system object to read the file, but otherwise same answer as itzme...

'add a reference to scripting runtime first
dim fso as new filesystemobject
dim ts as textstream
dim strLine as string
dim aryCells() as string

set ts=fso.openastextfile("c:\pathtofile\file.txt",1)

while not ts.atendofstream
   strLine=ts.readline()
   aryCells=split(strline,chr(9))

   'arycells should now be dimmed and filled with all the cell data for the current line.
   'do your writing to excel here.

wend


however i am sure there is an easier way if you wish to just use the excel object library, you can open the file directly in excel and it will do all the work for you, its faster too.
0
 

Author Comment

by:Sam8932
ID: 8160589
Ysgrathe,
That's what I planned on doing initially, but I don't think VBA supports FileSystemObject and TextStream.  Am I wrong about this?  I'm goint to try itzme's suggestion and I'll let you know how it goes.  Thanks guys.  I appreciate both of your quick responses.

Sam
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Ysgrathe
ID: 8162101
just for future reference (no pun intended!) you go to Tools>>References in the VBA code editor, there you can add any references you like, just like in VB6.
if its installed, and it should be, microsoft scripting runtime will be in the list that appears.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 8165162
Hi itzme,

Of course you can always use excel to do it all for you!

    Workbooks.OpenText Filename:= _
        "c:\myspreadsheets\mytabdelimfile.tdf", Origin:= _
        xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
        xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
        Comma:=False, Space:=False, Other:=False

One statement and it is done.


Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 

Author Comment

by:Sam8932
ID: 8174727
Thanks to all of you.  I'm sure all of your suggestions would work equally well.  I ended up using Ysgrathe's because that's what I'm most familiar with.  Thanks again.
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question