| 
| 
| 
| #205619 - 2012-08-31 04:39 PM    Re: REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix
[Re:  99osou] |  
| 99osou   Fresh Scripter
 
   Registered:  2007-03-15
 Posts: 6
 | 
FIRST OFF   MANY THANKS. I FEEL SO... DOH !
 Sometimes the simplest. Just could not get the structure right in the conversion. ARGH
 
 I'd like to share some additional EXCEL COM work I've managed to compile and use over the years.
 Most of it is simple but perhaps someone can fine them useful.
 Many are from talented coders that I've incorporated, esp the functions.
 
 Thanks again.
 
 auto fitting a range of cells
 
 $oXL.Range("A1:C1").EntireColumn.AutoFit
 writing a data set to an excel range:
 
 $ARRAYS = UCase($BKNASSOURCE),UCase($BKNASTRUNCSOURCE),UCase($NASARRAY[5])
$oXL.Range("A1:C1").Value = $ARRAYS
 an excel sort routine
 
 xlSort("A",,,)
Function xlSort($ByCol1,Optional $Order1,Optional $ByCol2,Optional $Order2,Optional $ByCol3,Optional $Order3)
	$xlHeader = 1 ; Sheet has a header 1=Yes 2=No
	Dim $ParmOk,$SortStartRow
	If $xlHeader = 1
		$SortStartRow = '2'
	Else
		$SortStartRow = '1'
	EndIf
	$ParmOk = 1
	For $i = 1 to 3 ; Parameter check
		$RC = Execute("
			If '' + $$Order$i = ''
				$$Order$i = 1 ; Default sortorder, ascending
			Else
				Select
					Case $$Order$i = 'A'
						$$Order$i = 1
					Case $$Order$i = 'D'
						$$Order$i = 2
					Case $$Order$i <> 1 And $$Order$i <> 2 ; Illegal parameter
						$$ParmOk = 0
				EndSelect
			EndIf
			$$ByCol$i = $$ByCol$i + '$SortStartRow'
		") ; End Execute
		If Not $ParmOk
		Return
	EndIf
	Next
	$RC = $oXl.Cells.Select
	If $ByCol2 <> $SortStartRow
		If $ByCol3 <> $SortStartRow
			$RC = $oXl.Selection.Sort($oXl.ActiveSheet.Range("$ByCol1"),$Order1,,
			$oXl.ActiveSheet.Range("$ByCol2"),$Order2,
			$oXl.ActiveSheet.Range("$ByCol3"),$Order3,$xlHeader)
		Else
			$RC = $oXl.Selection.Sort($oXl.ActiveSheet.Range("$ByCol1"),$Order1,,
			$oXl.ActiveSheet.Range("$ByCol2"),$Order2,,,$xlHeader)
		EndIf
	Else
		$RC = $oXl.Selection.Sort($oXl.ActiveSheet.Range("$ByCol1"),$Order1,,,,,,$xlHeader)
	EndIf
EndFunction
 
 this adds an excel sheet to an open workbook
 
 
 
xlAddSheet($oXL, '+Sheet2', 'SHARES', 33)
     Function xlAddSheet($_ID, OPTIONAL $_Position, OPTIONAL $_Name, OPTIONAL $_TabColor)
	Dim $_, $_Err
	If Left($_Position, 1) = '+'
		$_Position = SubStr($_Position, 2)		; trim leading '+'
		$_ = $_ID.WorkSheets.Add(, $_ID.WorkSheets($_Position), 1)
	Else
		If $_Position
			If Left($_Position, 1) = '-'
				$_Position = SubStr($_Position, 2)	; trim leading '-'
			EndIf
			$_ = $_ID.WorkSheets.Add($_ID.WorkSheets($_Position), , 1)
		Else
			$_ = $_ID.WorkSheets.Add
		EndIf
	EndIf
	$_Err = @ERROR				; save the creation status
	If $_Name
		$_ID.ActiveSheet.Name = $_Name		; define the name, if supplied
	EndIf
	If $_TabColor
		$_ID.ActiveSheet.Tab.ColorIndex = $_TabColor
	EndIf
	$xlAddSheet = $_ID.ActiveSheet.Name		; return the name of the new sheet
	EXIT $_Err
EndFunction
 
 some simple formatting and function code
 
 
	$oXL.ActiveWindow.FreezePanes = True
	$oXL.Range("A1:P1").Font.Bold = 1
	$oXL.Range("A1:P1").AutoFilter
	$oXL.Range("A1:P1").Interior.Colorindex = 15
	$oXL.Range("A1:P1").ColumnWidth = 42 
        $oXL.Range("E1").EntireColumn.Hidden = True
        $oXL.Range("P1:Q1").EntireColumn.AutoFit
        $oXL.APPLICATION.Calculation = 1  ; turns auto calc on/off. Great to turn it off. Do all your formatting then turn it on again before closing file. this way your not recalculating after each individual change.
        $oXL.Workbooks.Close
        $oXL.Quit
 various ways of saving excel files
 
 
Function xlSave($FileName)
		$RC = $oXl.ActiveWorkbook.SaveAs($FileName,-4158,"","",0,0,,,0)
		; (2003) xlworkbooknormal = -4143
		;       51 = xlopenxmlworkbook (without macro's in 2007-2010, xlsx)
		;	52 = xlopenxmlworkbookmacroenabled (with or without macro's in 2007-2010, xlsm)
		;	50 = xlexcel12 (excel binary workbook in 2007-2010 with or without macro's, xlsb)
		;	56 = xlexcel8 (97-2003 format in excel 2007-2010, xls)
		; ".csv": fileformatnum = 6 commas
		; ".txt": fileformatnum = -4158 tabs
		; ".prn": fileformatnum = 36
EndFunction 
 
 rename an excel sheet
 
 
 
Function xlSheetName($_ID, OPTIONAL $_Old, OPTIONAL $_New)
	; if Old name not provided, return the active sheet name
	If $_Old = ''
		$xlSheetName = $_ID.ActiveSheet.Name
		EXIT @ERROR
	EndIf
	; complain if New name isn't provided
	If VarType($_New) = 0
		EXIT 87
	EndIf
	; Rename the sheet
	$_ID.WorkSheets($_Old).Name = $_New
	EXIT @ERROR
EndFunction
 |  
| Top |  |  |  |  
 
 Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
 
 | 
| 
 
| 0 registered
and 793 anonymous users online. 
 | 
 |  |