ViciousXUSMC

Appending Many Excel Files Together

12 posts in this topic

So I am doing a custom inventory script that creates an excel document with 4 columns and sends a copy of the excel file over to a network location.

What I end up with is a few hundred excel files each representing a single computer.  My goal is to append all of those documents together into a single document so that I can turn the data into a table for sorting and review.

I know there are a few ways to skin this cat, just not sure the easiest and best.

I was trying using com objects based on an old thread I found by searching but I fail on my Line 10 when I try to declare the $master sheet I get not an object.

I am open to any solution, and interested in the com method as well since I am learning that stuff on the side.

My Broken Script
 

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>

$aFiles = _FileListToArray("\\vpsfs1\pstinfo\pst info\", "*.xls", $FLTA_FILES, TRUE)

;_ArrayDisplay($aFiles)

$oEX = ObjCreate("","excel.Application")
$master = $oEX.Workbooks.Open("\\vpsfs1\pstinfo\master.xlsx")

For $i = 1 to $aFiles[0]
$another = $oEX.Workbooks.Open($aFiles[$i])
$another.Activesheet.UsedRegion.Copy
$next = StringSplit($master.ActiveSheet.UsedRange.Address,"$")
$master.Activesheet.Range("a" & Activesheet.usedrange.rows.count + 1).Select
$master.Activesheet.Paste
Next

 

Share this post


Link to post
Share on other sites



use Excel UDF read to array and append all arrays.

 

Saludos


 

venezuela.png Would you like to say thank to Danyfirex using a warmy way?  offer me a cup of coffee... coffee.png

autoit_scripter_blue_userbar.png

       AutoIt...

 

 

Share this post


Link to post
Share on other sites

I second that. The UDF might be a bit slower but you get better error handling :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Working on it :) I should only use ExcelOpen() once and ExcelClose() at the end after my read loop correct?  Or will I need to open/close for each iteration of the loop?

Having a few issues, even when I put everything in the loop, this only gives me a result on the first pass.  Once I get it working then I need to figure out the best way to append this to an array.

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls", $FLTA_FILES, TRUE)
;_ArrayDisplay($aFiles)

For $i = 1 to $aFiles[0]
Local $oExcel = _Excel_Open(False)
Local $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
Local $aResult = _Excel_RangeRead($oWorkbook, DEFAULT, DEFAULT)
_Excel_Close($oExcel, False, True)
_ArrayDisplay($aResult)
Next

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This work For me.

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls",$FLTA_FILES, TRUE)


Local $oExcel = _Excel_Open(False)

For $i = 1 to $aFiles[0]
Local $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
Local $aResult = _Excel_RangeRead($oWorkbook, DEFAULT, DEFAULT)
_ArrayDisplay($aResult)
Next

_Excel_Close($oExcel, False, True)
close book if is needed with_Excel_BookClose

Saludos

Edited by Danyfirex
Edited

 

venezuela.png Would you like to say thank to Danyfirex using a warmy way?  offer me a cup of coffee... coffee.png

autoit_scripter_blue_userbar.png

       AutoIt...

 

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You should move _Excel_Open/_Excel_Close to the begin/end of the script (untested):

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls", $FLTA_FILES, TRUE)
Global $oWorkbook, $aResult
Global $oExcel = _Excel_Open(False)
$oMaster = _Excel_BookOpen("\\vpsfs1\pstinfo\master.xlsx")
For $i = 1 to $aFiles[0]
    $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
    $aResult = _Excel_RangeRead($oWorkbook)
    _Excel_RangeWrite($oMaster, Default, $aResult, "A" & $oMaster.ActiveSheet.UsedRange.Rows.Count + 1)
    _Excel_BookClose($oWorkBook, False)
Next
_Excel_Close($oExcel, False, True)

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Yes I had it that way first Water & Danny it was not working so I moved in in the loop for testing.

I think my issue maybe Excel related or something, at first my Excel_Open was not even working until I went and messed with Trust Center settings.

Edit: Also tried adding Book Close.

 

So it seems my issue needs to be investigated and its not because I am being dumb. 
 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

Could you please insert a COM error handler to your script so we get detailed error information? Please check the helpfile for ObjEvent.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I fixed it, I had to turn off protected mode for these Type 2 files that are created from the ArrayToXLS UDF on the forums.

I imagine when it goes to close/save the first file it messes up the rest in the loop.

 

This looks good, thanks guys!

#Include <File.au3>
#Include <Array.au3>
#Include <FileConstants.au3>
#Include <Excel.au3>

$aFiles = _FileListToArray(@ScriptDir, "*.xls",$FLTA_FILES, TRUE)
Local $oExcel = _Excel_Open(False)
Local $aFinalResults[0][4]

For $i = 1 to $aFiles[0]
Local $oWorkbook = _Excel_BookOpen($oExcel, $aFiles[$i])
Local $aResult = _Excel_RangeRead($oWorkbook)
;_ArrayDisplay($aResult)
_ArrayConcatenate($aFinalResults, $aResult)
Next

_ArrayDisplay($aFinalResults)
_Excel_Close($oExcel, False, True)

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

Do not forget to close the workbooks you opened again. Else you will end up with severel hundred of open workbooks eating up your memory.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Oh that forced excel close at the end will not close all the workbooks?

Share this post


Link to post
Share on other sites

It will. But why have hundreds of worksbooks open which you no longer need?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to store an entire row of a Excel workbook into an array.  The row index is stored in a variable.
      How can i do it?
      Thanks in advance for your support.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to write a value into an excel column.
      I know where it starts from, but i don't know what the end is, last non-empty cell.
      How can i get the number of last non-empty cell?
      Thanks in advance.
      Regards 
    • Nareshm
      By Nareshm
      Hi All,
      I have excel file like this
      and i want to cut cell/text from excel to other software.

       
      I have to cut the cell of B column one by one and past into other software
      If Winexists("No Data Found")
      then restore cuted cell and goto next/down side cell
      How to do it ?
    • water
      By water
      Extensive library to control and manipulate Microsoft Excel charts.
      Theads: General Help & Support - Example Scripts
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2017-07-21)
      None. The COM error handling related bugs have been fixed.
       
    • Mag91
      By Mag91
      Hey Community,
      cause im too new in the Auto it world i will try it with the your help. hopefully.
      I woud like to know how i can handle my Problem.
      ----
      I have a Excel Data with 362 random numbers.
      For Example:
      1166642335374 1172899897343
      .....
      this numbers are a part of the filepath ...example
      D:\Projekte\1166_64233_5374
      as u can see its the first number of the Excel data. After the first 4 numbers it shoud make a "_" than another 5 "_"
      This is my first question. How can i handle this to make it Shell execute.
       
      --------
      Second question:
      If i am in the path.
      For Example:
      D:\Projekte\1166_64233_5374
      the code shoud search for specific PDF Files.
      They are named like: 0050569E364B1ED79B900F73E62660EC.pdf
      the first 15 letters are always the same
      0050569E364B1ED
      when he found this data he has to copy it on a Folder on the Desktop.
      (There can also be 2 or 3 pdfs in one Folder with this letters)
      ----
      Please give me some help :-)