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

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

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

    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question about Excel, i have to create several charts one below the other dynamically.
      I have thought to use:
       
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count  
      And then to use it in this way:
      $Graph_position = "=Test1!A"&$iRowCount+2&":K"&$iRowCount+24 But it doesn't work with charts.
      Does anyone have a suggestion?
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi all,
      i have an empty csv file, i have a non formatted text file.
      What do i want to do?
      I want to automate the process "get external data" in Excel, i want to import the data from the text file and basically create a csv file with a specific character encoding.
      Is it possible with AutoIT?
       
    • breakbadsp
      By breakbadsp
      I  want to create a excel file from my script if it does not exist.
      _ExcelBookOpen throws error=2 if file does not exist, after this error i want to create new file at this point.
      can i use _FileCreate()?
      _Logger($sLogPath, "{INFO}------: Opening Excel File: " & $sExcelPath& "") While 1 Local $oExcelTestResult = _ExcelBookOpen($sExcelPath) If @error = 2 Then If not _FileCreate($sResExcelPath) Then MsgBox(0, "Error", "Error In Opening REsult Excel File: Error: " & String(@error)) _Logger($sLogPath, "{ERROR}------: Result Excel File does not exist.. tried to create new but :ERROR : " & String(@error) & "") ExitLoop Else _Logger($sLogPath, "{INFO}------: Result Excel File does not exist.. **Created New**: ") EndIf Else ExitLoop EndIf WEnd  
    • 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.