|
-
Jan 27th, 2009, 03:15 AM
#1
Thread Starter
Member
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
-
Jan 27th, 2009, 03:46 AM
#2
Re: Connect two xls files
Excel files are not text files. You cannot append one .xls file to another .xls like that.
-
Jan 27th, 2009, 04:02 AM
#3
Thread Starter
Member
Re: Connect two xls files
 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
-
Jan 27th, 2009, 05:48 AM
#4
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:
Sub appendall() destbk As Object, srcbk As Object, strtrow As Integer, mypath As String, srcfile As String mypath = "C:\interfaces\" Set destbk = Workbooks.Open("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 strtrow = destsht.UsedRange.Rows.Count + 1 Set srcbk = Workbooks.Open(mypath & srcfile) '******* if you want some header between added sheets, put next 2 lines destbk.range("a1") = srcfile strtrow = strtrow +1 '********* srcbk.Sheets("sheet1").UsedRange.Copy destbk.Sheets("sheet1").Range("a" & strtrow) srcbk.Close false ''Open destfile For Append As 1 ''Print #1, filestr ''Close 1 srcfile = Dir Loop destbk.Save destbk.Close 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
-
Jan 27th, 2009, 07:59 AM
#5
Thread Starter
Member
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
-
Jan 27th, 2009, 03:19 PM
#6
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
-
Jan 28th, 2009, 02:50 AM
#7
Thread Starter
Member
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
-
Jan 28th, 2009, 03:32 AM
#8
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
-
Jan 28th, 2009, 03:43 AM
#9
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
-
Jan 28th, 2009, 04:40 AM
#10
Thread Starter
Member
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.
-
Jan 28th, 2009, 04:49 AM
#11
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
-
Jan 28th, 2009, 07:05 AM
#12
Thread Starter
Member
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
-
Jan 28th, 2009, 07:22 AM
#13
Re: Connect two xls files
Please mark your thread as Resolved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|