Results 1 to 13 of 13

Thread: Connect two xls files

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    36

    Connect two xls files

    Dear all i want to connect together on the same sheet two different xls files. I am using the following code :

    mypath = "C:\interfaces\"
    destfile = "C:\interfaces\debitlist.xls"
    srcfile = Dir(mypath & "*.xls")
    Do While Len(srcfile) > 0
    Open mypath & srcfile For Input As 1
    filestr = Input(LOF(1), #1)
    Close 1
    Open destfile For Append As 1
    Print #1, filestr
    Close 1
    srcfile = Dir
    Loop

    but i am getting the following error
    INPUT PAST END OF FILE on the 6th line .
    Does anybody knows how can i do it.

    Thanks
    marinosandria

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Connect two xls files

    Excel files are not text files. You cannot append one .xls file to another .xls like that.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    36

    Re: Connect two xls files

    Quote Originally Posted by anhn
    Excel files are not text files. You cannot append one .xls file to another .xls like that.
    Can you please anyone help me how can i do it?

    Thanks

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Connect two xls files

    try like this, but i don't promise there will not be some blank rows between appended sheets
    vb Code:
    1. Sub appendall()
    2. destbk As Object, srcbk As Object, strtrow As Integer, mypath As String, srcfile As String
    3. mypath = "C:\interfaces\"
    4. Set destbk = Workbooks.Open("C:\interfaces\debitlist.xls")
    5. srcfile = Dir(mypath & "*.xls")
    6. Do While Len(srcfile) > 0
    7. ''Open mypath & srcfile For Input As 1
    8. ''filestr = Input(LOF(1), #1)
    9. ''Close 1
    10. strtrow = destsht.UsedRange.Rows.Count + 1
    11. Set srcbk = Workbooks.Open(mypath & srcfile)
    12. '******* if you want some header between added sheets, put next 2 lines
    13. destbk.range("a1") = srcfile
    14. strtrow = strtrow +1
    15. '*********
    16. srcbk.Sheets("sheet1").UsedRange.Copy destbk.Sheets("sheet1").Range("a" & strtrow)
    17. srcbk.Close false
    18. ''Open destfile For Append As 1
    19. ''Print #1, filestr
    20. ''Close 1
    21. srcfile = Dir
    22. Loop
    23. destbk.Save
    24. destbk.Close
    25. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    36

    Re: Connect two xls files

    Thanks for your advise, but to be more specific, i have several xls files in one folder called interfaces and i want to merge them all into on worksheet called debit list.xls.
    I have tried to many codes but i couldnt made it since now. I can do that for .txt and .csv files but not for .xls files.

    If anyone of you can help me i will be very greatfull.

    Thanks
    marinosandria

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Connect two xls files

    did you try the code i posted? did it give error or wrong result?
    if error what error and at which line?
    if wrong result what was incorrect?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    36

    Re: Connect two xls files

    Dear Westconn1 i have tried your code and i got an error on line 10. It doesnt recognize the variable destsht. That is logical because is not defined at the begining of the project. Shall i define it as an object?

    Thanks
    marinosandria

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Connect two xls files

    Try this:
    Code:
    Sub AppendAllSheet1s()
        Dim sPath   As String
        Dim wb0     As Workbook
        Dim ws0     As Worksheet
        Dim sFname0 As String
        Dim wb      As Workbook
        Dim ws      As Worksheet
        Dim sFname  As String
        Dim r       As Long
        
        sPath = "C:\interfaces\"
        sFname0 = "debitlist.xls"
        If Len(Dir(sPath & sFname0)) Then
            Set wb0 = Workbooks.Open(sPath & sFname0)
        Else
            Set wb0 = Workbooks.Add()
        End If
        Set ws0 = wb0.Worksheets(1)
        sFname = Dir(sPath & "*.xls")
        Do While Len(sFname)
            If sFname <> "debitlist.xls" Then
                r = ws0.UsedRange.Row + ws0.UsedRange.Rows.Count
                Set wb = Workbooks.Open(sPath & sFname, False, True)
                wb.Worksheets(1).UsedRange.Copy ws0.Cells(r, 1)
                wb.Close False
            End If
            sFname = Dir()
        Loop
        Set wb = Nothing
        If Len(wb0.Path) Then
            wb0.Save
        Else
            wb0.SaveAs sPath & sFname0
        End If
        'wb0.Close
        Set ws0 = Nothing
        Set wb0 = Nothing
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Connect two xls files

    i have tried your code and i got an error on line 10
    line 10 should have read
    strtrow = destbk.sheets("sheet1").UsedRange.Rows.Count + 1
    i changed some of the code, but missed that line
    of course you can declare and set a sheet object instead
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    36

    Re: Connect two xls files

    Dear anhn i have tried your code and its working fine. The only that is missing is the closing of the workbook , but i did by myself.

    Thank You Very Much, You Are Very Helpful.

    marinosandria
    Last edited by marinosandria; Jan 28th, 2009 at 04:51 AM.

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Connect two xls files

    at the end (where 'wb0.close) put wb0.activate and it should show to the user
    the only way to run an excel file is in excel, it is only a matter of tidying up any loose ends
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    36

    Re: Connect two xls files

    Dear all, i would like to thank you for your usefull information. After your help i have finished what i wanted to do.

    Thanks
    marinosandria

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Connect two xls files

    Please mark your thread as Resolved.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width