Vbs Merge multiple Excel files

Source: Internet
Author: User

CopyCode The Code is as follows: const xlworkbooknormal =-4143
Const xlsavechanges = 1
Objstartfolder = "C: \ test" 'directory of the source file to be read
Desexcel = "C: \ result1.xls" 'Final Summary Excel
Set excelapp = Createobject ("Excel. application ")
Set destbook = excelapp. workbooks. add' create an empty file
Set objfso = Createobject ("scripting. FileSystemObject") 'to create FileSystemObject
Set objfolder = objfso. getfolder (objstartfolder) 'to get the folder
Set colfiles = objfolder. Files'
Introw = 1' number of rows
For each objfile in colfiles 'process files in the folder in sequence
If ucase (right (TRIM (objfile. Name), 3) = "xls" then' only processes XLS files
Set srcbook = excelapp. workbooks. Open (objstartfolder + "\" + objfile. Name) 'Open the xls file

'Srcbook. worksheets (1). Copy destbook. worksheets (1)
Srcbook. Activate
Intcol = 1' Number of Columns
Do until excelapp. cells (1, intcol). value = ""
Tempdata = excelapp. cells (1, intcol). Value
Destbook. Activate
Excelapp. cells (introw, intcol). value = tempdata
Srcbook. Activate
Intcol = intcol + 1
Loop
Srcbook. Close 'Close the opened xls file
End if
Introw = introw + 1
Next
Destbook. saveas desexcel, xlworkbooknormal
Destbook. Close xlsavechanges
Excelapp. Quit

This method is OK

open an empty Excel file outside the directory where the file is stored
run the following macro: (Note the file directory) copy Code the code is as follows: Sub CFL ()
dim FS, f, F1, FC, S, x
set FS = Createobject ("scripting. fileSystemObject ")
set f = FS. getfolder ("E: \ test \") 'directory where the file is stored
set fc = f. files
for each F1 in FC
If right (f1.name, 3) = "xls" then
X = x + 1
workbooks. open (f1.path)
for I = 1 to 255
workbooks (1 ). sheets (1 ). cells (X, I ). value = _
workbooks (2 ). sheets (1 ). cells (1, I ). value
next
workbooks (2 ). close savechanges: = false
end if
next
end sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.