473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to append .xls file as sheet in new workbook from the specified folder

Hi all

I've created a some sheets of .xls file like
sample_sheet1.xls
sample_sheet2.xls
sample_sheet3.xls

Now i want to append these three sheets.xls in sample.xls main file in same folder.

I tried this same case for .csv files. It works. But when i tried with .xls files by using Macro i couldn't get.

Could you please anyone help me on this.

Thanks in advance
Jan 23 '10 #1
5 3285
jhardman
3,406 Expert 2GB
I assume the problem is that the function you are using pulls in individual sheets and puts them together into one xls file. CSVs are by definition one sheet only, so you had no problem, but XLS files can contain multiple sheets, so the function you are using is failing. Show the file you have and we will see if we can find a solution.

Jared
Jan 25 '10 #2
Hi,
Thanks for your guidance.

My Macro code is below
Expand|Select|Wrap|Line Numbers
  1. Sub GetExcelFileData() 
  2. Dim strFilePath As String, strFilename As String, strFullPath As String 
  3. Dim lngCounter As Long 
  4. Dim oConn As Object, oRS As Object, oFSObj As Object 
  5. Dim fileItem As Object 
  6.  
  7. Application.ScreenUpdating = False 
  8.  
  9. 'This gives us a full path name e.g. C:tempfolderfile.txt 
  10. 'We need to split this into path and file name 
  11. Set oFSObj = CreateObject("SCRIPTING.FILESYSTEMOBJECT") 
  12.  
  13. Set srcFolder = oFSObj.GetFolder(ThisWorkbook.Path) 
  14.  
  15. 'Open an ADO connection to the folder specified 
  16. Set oConn = CreateObject("ADODB.CONNECTION") 
  17. oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
  18. "Data Source=" & ThisWorkbook.Path & ";" & _ 
  19. "Extended Properties='Excel 8.0;HDR=Yes';" 
  20.  
  21. Set oRS = CreateObject("ADODB.RECORDSET") 
  22.  
  23. lngCounter = 1 
  24.  
  25. 'Now actually open the excel file and import into Excel 
  26. For Each fileItem In srcFolder.Files 
  27.  
  28. strFile = fileItem.Name 
  29.  
  30. If Right(fileItem.Name, 4) = ".xls" And InStr(fileItem.Name, Left(strFile, InStr(strFile, ".") - 1)) = 1 Then 
  31.  
  32. oRS.Open "SELECT * FROM " & strFile, oConn, 3, 1, 1 
  33. While Not oRS.EOF 
  34. If lngCounter > 1 Then 
  35. Sheets.Add After:=Worksheets(Worksheets.Count) 
  36. Else 
  37. lngCounter = 2 
  38. End If 
  39.  
  40. ActiveSheet.Range("A1").CopyFromRecordset (oRS) 
  41. Wend 
  42. oRS.Close 
  43. Columns("A:IV").AutoFit 
  44. End If 
  45. Next 
  46.  
  47. oConn.Close 
  48.  
  49. ActiveWorkbook.Saved = True 
  50.  
  51. End Sub
  52.  
Actually i created the sample_sheets.xls files using HTML tags and trying to append these sheets in main sample.xls file as a sheets.
Feb 3 '10 #3
jhardman
3,406 Expert 2GB
OK, then what I told you earlier is definitely what is going on, I've used the ADODB.recordet to open an excel file before, but never like this. Basically, the ADODB object is looking for something it can interpret as a data table. CSV files are very simple data tables, so that gave you no problem, but the xls files are not. If you want to use the ADODB method then you will need to specify the sheet name. I think instead you will want to use a different object. Let me see if I can find some code for you.

Jared
Feb 3 '10 #4
Thank for your reply and waiting for the solution.
Feb 4 '10 #5
jhardman
3,406 Expert 2GB
I'm sorry Koksamsun, I was reading through your code just now, feeling guilty for not getting to it sooner, and I realized that you didn't post in the right forum. when people post code in C# it's obvious, but you were writing in VB.NET which is close enough to VBScript that I didn't even notice. I assume you want this in the ASP.NET forum? This is the classic ASP forum (only answers in VBScript or possibly JScript if anyone really begs).

Jared
Feb 19 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: dgoel | last post by:
Hi, I Have a text file & I want to open it in excel sheet ( withou importing). I have written code for it, but it is not opening exce sheet. It opens the text file, but does not create a excel...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
1
by: Boomessh | last post by:
Hi, I am trying to open an existing excel workbook. It pops up an error stating that “Errors were detected in 1598.xls but Microsoft Office Excel was able to open the file by making the repairs...
1
by: Peter Stojkovic | last post by:
I am importing data from an EXCEL XLS-File via OLEDB-Provider The command is SELECT * from Everything works fine if the first sheet is named sheet1 But sometimes the name is not known...
0
by: Anish G | last post by:
Hi All, I am getting the below given error while running my application in live server. In my local machine, its working fine. Please help me as it is very urgent for me. Exception from...
2
by: RAGHAVENDRAS | last post by:
Hi, I am writing a script which should update an excel sheet daily. So how do I determine which is the last row which contains data and then write the latest data into the sheet. use...
6
by: poolboi | last post by:
hi all, i've got the following program that needs yr help: use Win32::OLE; # use existing instance if Excel is already running eval {$ex =...
2
by: deve8ore | last post by:
Hello, I've already created a dropdown so an end user can pull up a specific file, and this will create a link to a variety of files (.xls, .txt., .pdf, ect...). I'd like to set about twenty...
16
emibt08
by: emibt08 | last post by:
Hello fellow programmers. I am working on a project that is a windows service. It should open an excel file and read it and it worked good on WinXP SP2 w/ Office 2003. Now the customer upgraded to...
8
blazedaces
by: blazedaces | last post by:
So I have a program below which writes an excel file with multiple sheets based on inputs of sheet names, data, cell types, etc. It uses Apache POI, which is currently the only thing I found...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.