VBS merge scripts for multiple Excel files _vbs

Source: Internet
Author: User
Copy Code code as follows:

Const xlWorkbookNormal =-4143
Const xlsavechanges = 1
Objstartfolder = "C:\Test" ' Directory of source files to read
Desexcel= "C:\result1.xls" ' last generated rollup Excel
Set Excelapp = CreateObject ("Excel.Application")
Set Destbook = ExcelApp.Workbooks.Add ' Creates an empty file
Set objFSO = CreateObject ("Scripting.FileSystemObject") ' establishes FileSystemObject
Set objfolder = Objfso.getfolder (objstartfolder) ' Get folder
Set colfiles = Objfolder.files ' Get all files in Source directory
Introw=1 ' Number of rows
For each objfile in Colfiles ' to sequentially process files in a folder
If UCase (Right (Trim (Objfile.name), 3) = "xls" Then ' only handles XLS files
Set Srcbook = ExcelApp.Workbooks.Open (Objstartfolder + "\" + objfile.name) ' Open 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 ' Closes the XLS file that is already open
End If
Introw=introw+1
Next
Destbook.saveas Desexcel,xlworkbooknormal
Destbook.close xlsavechanges
Excelapp.quit

This method ok

Open an empty Excel document outside of the directory where the file resides
Run the following sub-macro: (Note the file directory)
Copy Code code as follows:

Sub CFL ()
Dim FS, F, F1, FC, S, X
Set fs = CreateObject ("Scripting.FileSystemObject")
Set f = fs. GetFolder ("e:\test\") ' Directory for storing files
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.