• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Import Text and Export as Excel

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
Marcusw
Asked:
Marcusw
  • 8
  • 7
1 Solution
 
itzmeCommented:
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
 
MarcuswAuthor Commented:
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
 
itzmeCommented:

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!

 
MarcuswAuthor Commented:
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
 
MarcuswAuthor Commented:
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
 
itzmeCommented:
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
 
MarcuswAuthor Commented:
fantastic i thought i would have to research this for weeks.

i've also increased your points
0
 
itzmeCommented:
Do you want it to run faster? I think I might have that one in  a couple mins(maybe ten or fifteen)?
0
 
MarcuswAuthor Commented:
that would be very useful because i sometime have files of up to 50,000 records
0
 
itzmeCommented:
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
 
MarcuswAuthor Commented:
that would be very useful because i sometime have files of up to 50,000 records
0
 
MarcuswAuthor Commented:
i get an error

user defined type not type


on the Dim fso As New FileSystemObject
0
 
itzmeCommented:
Sorry forgot to say got to project->references "Microsoft Scripting Runtime".
0
 
MarcuswAuthor Commented:
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
 
itzmeCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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