Vbscript to concatenate excel files of different names?

Posted on 2008-06-11
Last Modified: 2011-10-19
Hello Experts,

I trying to concatentate the contents of all excel files in a certain folder into a resultant excel file

All files I want to concatenate to result.xls start with the letters logXXXX2008 where XXXX is the day and month. These are different in each file.

Can this be done in vbscript?
Question by:grace101
LVL 39

Expert Comment

ID: 21762768
does it have to be vbs? Could it be vba?

Do your excel files have more than one sheet?
Do you want to end up with many worksheets or just one with all the data?

Expert Comment

ID: 21766195
This can indeed be done using vbs.
How's the contents of the files structured?
What's the intended structure of the final file?

(Do you simply wish to pack all sheets to a single workbook?)

Author Comment

ID: 21769414
Thank you for your responses.

I'm using vbs because ive sort of half used it before. (No expert though by a long shot!)

Ive attached an example of one of the files i want to process. There are multiple files in the same folder (as i said before with different timestamps) that i want to
concatenate together.

The data in the sample file is temperature data from 7 ovens. Im interested in Oven 3. (Column C of each spreadsheet)

I'd like to concatenate all values from column C of each spreadsheet file using a vbscript and place the result in a text file. Result .txt.

Is something like this possible?


Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 65

Expert Comment

ID: 21790439
Hi Grace,

Try this VBS file.

Just change this line to suit the path to your XLS files:
strFolder = "C:\TEMP\Temp\Test script\Excel"

and it should then create a new Results.xls file in the same folder as the VBS file.


strFolder = "C:\TEMP\Temp\Test script\Excel"
strResultFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Results.xls"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
Const xlUp = -4162
objExcel.Visible = True
objExcel.ScreenUpdating = False
Set objResultWB = objExcel.Workbooks.Add
For Each objFile In objFSO.GetFolder(strFolder).Files
	If Left(LCase(objFile.Name), 3) = LCase("Log") And Right(LCase(objFile.Name), 4) = LCase(".xls") And LCase(objFile.Name) <> LCase(strResultFile) Then
		intRow = objResultWB.Sheets(1).Cells(65536, "C").End(xlUp).Row
		If intRow > 1 Then intRow = intRow + 1
		Set objSourceWB = objExcel.Workbooks.Open(objFile.Path, False, False)
		objSourceWB.Sheets(1).Range("C1:C" & objSourceWB.Sheets(1).Cells(65536, "C").End(xlUp).Row).Copy objResultWB.Sheets(1).Range("C" & intRow)
		objSourceWB.Close False
	End If
objExcel.DisplayAlerts = False
objResultWB.SaveAs strResultFile, True
objExcel.DisplayAlerts = True
objExcel.ScreenUpdating = True

Open in new window


Author Comment

ID: 21793799
Thanks very much Rob for posting that:-)

Can you show me how to modify it so that it ll concatenate files starting with log  and ending in a variable number of (characters/numbers etc..) for future reference?
I have other files that id like to concatenate that dont all necessarily include the timestamp "XXXX2008" that id like to use this script on in the future:-) (Ive already spent 2 hours attempting this modification but have failed miserably!)

Thanks, Grace
LVL 65

Expert Comment

ID: 21797576
Hi Grace,

So far, the only line that does any validation of the file name itself is this line:
If Left(LCase(objFile.Name), 3) = LCase("Log") And Right(LCase(objFile.Name), 4) = LCase(".xls") And LCase(objFile.Name) <> LCase(strResultFile) Then

Which will pick up any file that start with "log" (by the Left function), and ends with ".xls" (by the Right function).  Currently it does not validate anything else in the middle,  so it should pick up

Does that not work for you?



Author Comment

ID: 21801288
Sorry for the delay Rob,

Ive finally figured out those other files I wish to concatenate are comma seperated value files. Can your script be modified to handle those too please?

Thanks very much for your patience:-)
LVL 65

Accepted Solution

RobSampson earned 500 total points
ID: 21809878
See if it works as is, with CSV files, by changing this line:

If Left(LCase(objFile.Name), 3) = LCase("Log") And Right(LCase(objFile.Name), 4) = LCase(".xls") And LCase(objFile.Name) <> LCase(strResultFile) Then

to this
If Left(LCase(objFile.Name), 3) = LCase("Log") And (Right(LCase(objFile.Name), 4) = LCase(".xls") Or Right(LCase(objFile.Name), 4) = LCase(".csv"))And LCase(objFile.Name) <> LCase(strResultFile) Then



Author Closing Comment

ID: 31466205
That did the job. Thanks very much Rob.

That script will come in very handy:-)
LVL 65

Expert Comment

ID: 21818008
No problem.  Thanks for the grade.



Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question