;*************************************************************************
; Script Name: FindManagers.kix
; Author: Wim Rotty
; Date: 26/09/2007
; Description: Copy and paste data from one Excel book to other books
;*************************************************************************
;Script Options
If Not @LOGONMODE
Break On
Else
Break Off
EndIf
Dim $RC
$RC = SetOption("Explicit", "On")
$RC = SetOption("NoMacrosInStrings", "On")
$RC = SetOption("NoVarsInStrings", "On")
If @SCRIPTEXE = "KIX32.EXE"
$RC = SetOption("WrapAtEOL", "On")
EndIf
;Declare variables
Dim $MyTable
Dim $objExcel
Dim $Row, $i, $Manager, $Managers[0], $j
;Initialize variables
$MyTable = "C:\Test\Tabel.xls"
$objExcel = CreateObject("Excel.Application")
If @ERROR
Exit @ERROR
EndIf
$j = 0
;Code
;Do not show alerts like messages about overwriting files
$objExcel.DisplayAlerts = False
;Show Excel, not really needed if you quit at the end
;$objExcel.Visible = -1
;Open file
$RC = $objExcel.Workbooks.Open($MyTable)
;Add filter
$RC = $objExcel.Selection.Autofilter
;Select data range (cell at bottom right of data range)
$RC = $objExcel.Range("A1:P1285")
;Get cell rownumber
$Row = $objExcel.ActiveCell.Row
;Recurse cells up to cell 2 and gather all the Manager names in an array
For $i = $row to 2 step -1
;Get the text (manager name) in the cell
$Manager = $objExcel.Range("K"+$i).Text
;Is the manager name in the array?
If AScan($Managers, $Manager) = -1
;If not, add location to array and add it to new location
ReDim Preserve $Managers[$j]
$Managers[$j] = $Manager
$j = $j + 1
EndIf
Next
;Select all data in range
$RC = $objExcel.Range($objExcel.Selection, $objExcel.Cells(1)).Select
;Recurse the Managers array
For Each $Manager in $Managers
;Filter for each manager name (where "11" is the number of manager column)
$RC = $objExcel.Selection.AutoFilter(11, $Manager)
;Copy the selection to the clipboard
$RC = $objExcel.Selection.Copy
;Add a new workbook
$RC = $objExcel.Workbooks.Add
;Paste the data from the clipboard
$RC = $objExcel.ActiveSheet.Paste
;Select columns
$RC = $objExcel.Columns("D:O").Select
;Hide selected columns
$objExcel.Selection.EntireColumn.Hidden = True
;AutoFit columns
$RC = $objExcel.Columns("A:C").EntireColumn.AutoFit
;$RC = $objExcel.Selection.EntireColumn.Hidden
;Save the workbook with as name the manager
$RC = $objExcel.ActiveWorkbook.SaveAs@SCRIPTDIR+"\"+$Manager+".xls",,,,,False)
;The next active sheet is the original file
$RC = $objExcel.ActiveWindow.ActivateNext
Next
;Close Excel
$RC = $objExcel.Application.Quit
;Personal UDF Section
;UDF Section