Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Excel-Importing a Tab delimited file usig VBA

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
Sam8932
Asked:
Sam8932
1 Solution
 
itzmeCommented:
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
 
YsgratheCommented:
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
 
Sam8932Author Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
YsgratheCommented:
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
 
TimCotteeCommented:
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
 
Sam8932Author Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now