Solved

Txt files to Excel report

Posted on 2003-12-01
7
334 Views
Last Modified: 2010-05-03
Hello,

I have three txt files, say file1, file2 and file3.

File1 has format like:

Name1     Data1
Name2     Data2
Name3     Data3
Name 4    Data4

File2 also has the same format, just that sometimes  Names could be more or less than file1

Name1    text1
Name3    text3
Name4    text4  
Name5   text5
(here Name2 is missing, but Name5 is added)

File3 also has the same format, just that sometimes it can have Names that are more or less than file1 and file2.

Name1   variable1
Name2   variable2
Name6    variable6
(Name3, Name4, Name5 missing and Name6 added).

I want to capture all the Names present in all the three files and format it into one output file with,
Name, Data, Text, Variable columns and show these as 4 columns in an excel spreadsheet.

My output in excel will look like:

Name1   Data1   text1   variable1  
Name2   Data2   -         variable2
Name3   Data3   text3   -  
Name4   Data4   text4   -
Name5   -          text5   -
Name6   -          -         variable6

Please advise.

Thanks

0
Comment
Question by:kamur
[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
7 Comments
 
LVL 1

Expert Comment

by:MsLim
ID: 9855811
Based on your subject txt file to excel , BUT based on your column asking in VB programming.
are you interest to use programming to combine it become one ?
if yes , then read in line by line from file save it into database  with Name and data.
upon complete , read line by line from file 2, search the Name with database . if exist update the field call text otherwise append new record for such name and text.
upon complete file 2, read line by line from file 3 , search the Name with databse , if exist update the field call variable , otherwise append new record for such name and variable.

Hope can give ideal for you .
0
 
LVL 1

Expert Comment

by:anand2k
ID: 9855928
HI,

The other way u can do if u do not want use database then use array/collection for each file and load the data into it with one for final output.

U just have to add a logic to search in array/collection to not get duplicate.

Enjoy
anand
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 9856660
Some Remarks on this little procedure (copy and paste it: tools-macro-visual basic editor, doubleclick on this sheet)
1: dim strArray(5,3) --> it's possible to adjust the first number to the maximum in different names !
2: Open "c:\file1.txt"  --> set to the correct path (also file2 & file3)
3: I assumed that's ok that I set the data from cell A1 until....
4: I assumed that the data in the textfiles where separated by a ";" (fe: name1;data1)

Sub ImportFiles()
    Dim strArray(10, 3) As String
    Dim sTextLine As String
    Dim iFileNum As Integer
    Dim i, j As Integer
    Dim found As Boolean
    i = 0
    iFileNum = FreeFile
    ' IMPORT THE FIRST FILE INTO AN ARRAY
    Open "c:\file1.txt" For Input As #iFileNum   ' Open file.
    Line Input #iFileNum, textline
    Do While Not EOF(iFileNum)   ' Loop until end of file.
           ' Read line into variable.
        strArray(i, 0) = Left(textline, InStr(textline, ";") - 1)
        strArray(i, 1) = Mid(textline, InStr(textline, ";") + 1)
        i = i + 1
        Line Input #iFileNum, textline
    Loop
    Close #iFileNum   ' Close file.
    ' IMPORT THE SECOND FILE INTO AN ARRAY
    iFileNum = FreeFile
    Open "c:\file2.txt" For Input As #iFileNum   ' Open file.
    Do While Not EOF(iFileNum)   ' Loop until end of file.
        Line Input #iFileNum, textline   ' Read line into variable.
        j = 0
        found = False
        While j < UBound(strArray)
            If strArray(j, 0) = Left(textline, InStr(textline, ";") - 1) And strArray(j, 0) <> "" Then
               found = True
               num = j
               j = UBound(strArray)
            End If
            j = j + 1
        Wend
        If found = True Then
            strArray(num, 2) = Mid(textline, InStr(textline, ";") + 1)
        Else
            strArray(i, 0) = Left(textline, InStr(textline, ";") - 1)
            strArray(i, 1) = Mid(textline, InStr(textline, ";") + 1)
            i = i + 1
        End If
    Loop
    Close #iFileNum   ' Close file.
   
    ' IMPORT THE THIRD FILE INTO AN ARRAY
    iFileNum = FreeFile
    Open "c:\file3.txt" For Input As #iFileNum   ' Open file.
    Do While Not EOF(iFileNum)   ' Loop until end of file.
        Line Input #iFileNum, textline   ' Read line into variable.
        j = 0
        found = False
        While j < UBound(strArray)
            If strArray(j, 0) = Left(textline, InStr(textline, ";") - 1) And strArray(j, 0) <> "" Then
               found = True
               num = j
               j = UBound(strArray)
            End If
            j = j + 1
        Wend
        If found = True Then
            strArray(num, 3) = Mid(textline, InStr(textline, ";") + 1)
        Else
            strArray(i, 0) = Left(textline, InStr(textline, ";") - 1)
            strArray(i, 3) = Mid(textline, InStr(textline, ";") + 1)
            i = i + 1
        End If
    Loop
    Close #iFileNum   ' Close file.
    i = 0
    j = 0
    While strArray(i, 0) <> "" And i < UBound(strArray)
        For j = 0 To 3
            Me.Cells(i + 1, j + 1) = strArray(i, j)
        Next j
        i = i + 1
    Wend

End Sub
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:kamur
ID: 9858426
Dhaest,

I am getting the results as:

Name1      Data1      text1      variable1
Name2      Data2            variable2
Name3      Data3      text3      
Name4      text4              
Name5      text5            
Name6                  variable6

text4 and text5 are in wrong column.

Thanks

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 9858487
My mistake...

' This line must change (file1.txt)
            strArray(i, 2) = Mid(textline, InStr(textline, ";") + 1)

COMPLETE CODE


Sub ImportFiles()
    Dim strArray(10, 3) As String
    Dim sTextLine As String
    Dim iFileNum As Integer
    Dim i, j As Integer
    Dim found As Boolean
    i = 0
    iFileNum = FreeFile
    ' IMPORT THE FIRST FILE INTO AN ARRAY
    Open "c:\file1.txt" For Input As #iFileNum   ' Open file.
    Line Input #iFileNum, textline
    Do While Not EOF(iFileNum)   ' Loop until end of file.
           ' Read line into variable.
        strArray(i, 0) = Left(textline, InStr(textline, ";") - 1)
        strArray(i, 1) = Mid(textline, InStr(textline, ";") + 1)
        i = i + 1
        Line Input #iFileNum, textline
    Loop
    Close #iFileNum   ' Close file.
    ' IMPORT THE SECOND FILE INTO AN ARRAY
    iFileNum = FreeFile
    Open "c:\file2.txt" For Input As #iFileNum   ' Open file.
    Do While Not EOF(iFileNum)   ' Loop until end of file.
        Line Input #iFileNum, textline   ' Read line into variable.
        j = 0
        found = False
        While j < UBound(strArray)
            If strArray(j, 0) = Left(textline, InStr(textline, ";") - 1) And strArray(j, 0) <> "" Then
               found = True
               num = j
               j = UBound(strArray)
            End If
            j = j + 1
        Wend
        If found = True Then
            strArray(num, 2) = Mid(textline, InStr(textline, ";") + 1)
        Else
            strArray(i, 0) = Left(textline, InStr(textline, ";") - 1)
            strArray(i, 2) = Mid(textline, InStr(textline, ";") + 1)
            i = i + 1
        End If
    Loop
    Close #iFileNum   ' Close file.
   
    ' IMPORT THE THIRD FILE INTO AN ARRAY
    iFileNum = FreeFile
    Open "c:\file3.txt" For Input As #iFileNum   ' Open file.
    Do While Not EOF(iFileNum)   ' Loop until end of file.
        Line Input #iFileNum, textline   ' Read line into variable.
        j = 0
        found = False
        While j < UBound(strArray)
            If strArray(j, 0) = Left(textline, InStr(textline, ";") - 1) And strArray(j, 0) <> "" Then
               found = True
               num = j
               j = UBound(strArray)
            End If
            j = j + 1
        Wend
        If found = True Then
            strArray(num, 3) = Mid(textline, InStr(textline, ";") + 1)
        Else
            strArray(i, 0) = Left(textline, InStr(textline, ";") - 1)
            strArray(i, 3) = Mid(textline, InStr(textline, ";") + 1)
            i = i + 1
        End If
    Loop
    Close #iFileNum   ' Close file.
    i = 0
    j = 0
    While strArray(i, 0) <> "" And i < UBound(strArray)
        For j = 0 To 3
            Me.Cells(i + 1, j + 1) = strArray(i, j)
        Next j
        i = i + 1
    Wend

End Sub

0
 

Author Comment

by:kamur
ID: 9859106
Thanks Dhaest. It worked great.

Can I ask one more question on this...If I want to add a title at the top for each column, would that mess up the whole logic? Just a thought, not needed if it would be a major rewrite of the above code.

Thanks for the quick replies.
Regards


0
 
LVL 53

Expert Comment

by:Dhaest
ID: 9859549
You only have to adjust the last part of the code:

i = 0
j = 0
While strArray(i, 0) <> "" And i < UBound(strArray)
    For j = 0 To 3
        Me.Cells(i + 1, j + 1) = strArray(i, j)
    Next j
    i = i + 1
Wend
Change this: Me.Cells(i + 1, j + 1) = strArray(i, j)
Into: Me.Cells(i + 2, j + 1) = strArray(i, j)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 70
Search combo error "Data Type Mismatch in Criteria Expression" 2 94
Set email body to html using vbscript 6 87
Excel Automation VBA 19 115
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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 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