?
Solved

Import Text and Export as Excel

Posted on 2003-03-28
15
Medium Priority
?
202 Views
Last Modified: 2010-05-01
how would i create a program that once can import a text file and convert it to and excel preferabley without using either excel or access



0
Comment
Question by:Marcusw
[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
  • 8
  • 7
15 Comments
 

Expert Comment

by:itzme
ID: 8225156
How is the file split into different groups??
You'd split it depending on if its ",","vbTab" and so on and then a for loop that inputs for each line of the text file into a row in an excel spreadsheet. Do you want some code?(need to know how the delimiter of the file)
0
 

Author Comment

by:Marcusw
ID: 8225396
the input file will have a field delimiter of ,(comma) and  a text qualifier of " i.e.

"field1","field2","field3"

each record would start on a new line.

Something i didn't metion was that i wanted to convert the data to excel xp version

thanks
0
 

Accepted Solution

by:
itzme earned 800 total points
ID: 8225761

This will do what you want you'll need to add the formatting of the different rows yourself and also you need to include the "Microsoft Excel xx.x Object Library" through  format->references:

Private Sub Form_Load()
    Dim temp As String
    Dim rowNo As Integer
    Dim i As Integer
    Dim filePath As String
    Dim xlRow() As String
    Dim xla As Excel.Application
    Dim xlw As Excel.Workbook
    Dim xls As Excel.Worksheet
   
    Set xla = CreateObject("Excel.Application")
    Set xlw = xla.Workbooks.Add
    Set xls = xlw.Sheets("Sheet1")
    rowNo = 1
    xla.Visible = False
   
    filePath = "c:\test\file.txt"
   
    Open filePath For Input As #1
    While Not EOF(1)
        Line Input #1, temp$
        xlRow = Split(temp, ",", -1, vbTextCompare)
        For i = 0 To UBound(xlRow)
            xls.Cells(rowNo, i + 1) = xlRow(i)
            Debug.Print xlRow(i)
        Next i%
        rowNo = rowNo + 1
    Wend
    Close #1
   
    xlw.SaveAs ("c:\test\file.xls")
    xla.Quit
    Set xla = Nothing
    Unload Me
End Sub
This will save the file and put everything into a spreedsheet with a new row for every new line in the textfile.
0
Technology Partners: 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!

 

Author Comment

by:Marcusw
ID: 8225920
thanks for your code just one problem.

it creates a excel file but it leaves the " around the fields

so in cell 1 for instance it says "field1" not field1
0
 

Author Comment

by:Marcusw
ID: 8225935
also why does it take so long to create the file, ie a couple of minutes for a 5000 record file when access can produce the file in 5 seconds?
0
 

Expert Comment

by:itzme
ID: 8225974
Not a problem change the For loop to the one below:

     For i = 0 To UBound(xlRow)
            xlRow(i) = Replace(xlRow(i), Chr(34), "")
            xls.Cells(rowNo, i + 1) = xlRow(i)
            Debug.Print xlRow(i)
     Next i%

chr(34) is ->"<- but your not always able to put in """.
0
 

Author Comment

by:Marcusw
ID: 8225997
fantastic i thought i would have to research this for weeks.

i've also increased your points
0
 

Expert Comment

by:itzme
ID: 8226059
Do you want it to run faster? I think I might have that one in  a couple mins(maybe ten or fifteen)?
0
 

Author Comment

by:Marcusw
ID: 8226077
that would be very useful because i sometime have files of up to 50,000 records
0
 

Expert Comment

by:itzme
ID: 8226102
I'm only testing on small text file (5 records) and theres a very big speed diff. Tell me if its any better
Ok this should do it:
/**********************************************/
Private Sub Form_Load()
    Dim fso As New FileSystemObject
    Dim ts As TextStream
    Dim rowNo As Integer
    Dim i As Integer
    Dim xlRow() As String
    Dim xla As Excel.Application
    Dim xlw As Excel.Workbook
    Dim xls As Excel.Worksheet
   
    Set xla = CreateObject("Excel.Application")
    Set xlw = xla.Workbooks.Add
    Set xls = xlw.Sheets("Sheet1")
    rowNo = 1
    xla.Visible = False

    Set ts = fso.OpenTextFile("c:\test\file.txt")
    Do While ts.AtEndOfStream <> True
        xlRow = Split(ts.ReadLine, ",", -1, vbTextCompare)
        For i = 0 To UBound(xlRow)
            xlRow(i) = Replace(xlRow(i), Chr(34), "")
            xls.Cells(rowNo, i + 1) = xlRow(i)
        Next i%
        rowNo = rowNo + 1
    Loop
    ts.Close
   
    xlw.SaveAs ("c:\test\file.xls")
    xla.Quit
    Set xla = Nothing
    Unload Me
End Sub
0
 

Author Comment

by:Marcusw
ID: 8226115
that would be very useful because i sometime have files of up to 50,000 records
0
 

Author Comment

by:Marcusw
ID: 8226150
i get an error

user defined type not type


on the Dim fso As New FileSystemObject
0
 

Expert Comment

by:itzme
ID: 8226186
Sorry forgot to say got to project->references "Microsoft Scripting Runtime".
0
 

Author Comment

by:Marcusw
ID: 8231815
i just tried your new code, it works when i use 25 records but when i tried it on a 5000 record file i got the error

Application defined or object defined error

it highlights the line of code below

xls.Cells(rowNo, i + 1) = xlRow(i)

0
 

Expert Comment

by:itzme
ID: 8237253
Sorry was away for last couple days, If you wanna get around that prob you should change the "rowNo" and "i" variables to Longs as the range of integers is only
-32,768 to 32,767
so thats why it gives you an error on a 50,000 record file
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

762 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