Page 1 of 1 1
Topic Options
#205614 - 2012-08-30 05:56 PM REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix
99osou Offline
Fresh Scripter

Registered: 2007-03-15
Posts: 6
Please any help.

I am trying to Remove duplicates from an excel Spreadsheet I generate. I've looked up the method via MSDN and tried to translate it to KIX. I've also Googled and found nothing specific to my issue.

Per MSFT the default VB basic method is as follows:

ActiveSheet.Range("A1:C100").RemoveDuplicates Columns:=Array(1,2), Header:=xlYes

Column and Header are both Optional, I omitted column for debugging and also since I wish to use all columns, but as I have a header I've used it in my code as follows:

$xlHeader = 1

$rc=$oxl.Range("A1:C$EXCELROW8").RemoveDuplicates(,$xlHeader)


others combo's I've tried:
$rc=$oxl.Range("A1:C$EXCELROW8").RemoveDuplicates(,,$xlHeader)
$rc=$oxl.Range("A1:C$EXCELROW8").RemoveDuplicates(,,$xlHeader,)
$rc=$oxl.Range("A1:C$EXCELROW8").RemoveDuplicates(,,,$xlHeader)
$rc=$oxl.Range("A1:C$EXCELROW8").RemoveDuplicates(,,,$xlHeader,)


My variable - $EXCELROW8 is simply the end of my data set.

I've tried several permutations all for naught. I can only assume I've got the structure totally wrong or it requires a bit more then I'm guessing at. I have found many examples of sorting, formatting and other code port overs, which I've tweaked and used successfully, but nothing relating to this and it has me stumped.

I receive no errors unless I make a deliberate typo but the code does not work. So I'm missing something and grabbing at straws.

Any help or suggestions would be greatly welcomed.

Cheers and Thanks

Top
#205615 - 2012-08-30 06:13 PM Re: REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix [Re: 99osou]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
You might provide more of your script(the parts where you create the object, etc) so others can try it, along with some sample data to go along with it. Also what version of Excel and Kix are you using?
Top
#205616 - 2012-08-30 06:45 PM Re: REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix [Re: 99osou]
99osou Offline
Fresh Scripter

Registered: 2007-03-15
Posts: 6
Sorry my bad. There quite a bit of code to get to this point but I'm using win 7 64bit, latest wKix 4.62 and call Excel 2010 in kix via

 Code:
$oXL = CreateObject("EXCEL.application")


While I can't include the actual data any single column of data with at least two duplicates can suffice as a test bed.

This bit of code I use to create in Excel, the Sheet BACKEND after CENSUS,

 Code:
xlAddSheet($oXL, '+BACKEND', 'CENSUS', 33)
$LINEOUTPUTFC = "Path","Size","Free"
$oXL.Range("A1:C1").Value = $LINEOUTPUTFC
$oXL.Cells(2,1).Select
$oXL.ActiveWindow.FreezePanes = True
$oXL.Range("A1:C1").Font.Bold = 1
$rc = $oXL.Range("A1:C1").AutoFilter
$oXL.Range("A1:C1").Interior.Colorindex = 15
$oXL.Range("A1:C1").ColumnWidth = 42


the following is where I get the data from 4 separate files and combine them into one Excel sheet BACKEND
and add some formulas:

 Code:
:STARTOFVIPACEXPORT

$rc= $oxL.WorkSheets("BACKEND").Activate

$ARFILES = 1,2,3,4
For Each $ARF IN $ARFILES
  Open (7,"J:\xxxxxxxx\_-SCRIPTS-_\arx0" + $ARF + "@@xx.com - arx_exports_info.csv") 
  $VEDETAILS = ReadLine (7) ;DUMMY HEADER READ
  $VEDETAILS = ReadLine (7) ;1ST ACTUAL DATA LINE
  $VEDETAILS = Replace($VEDETAILS,'"',"")
  
  While @ERROR = 0
  	$VEARRAYS = Split($VEDETAILS,",")
  	$VEPATH = $VEARRAYS[1] + $VEARRAYS[3]
  	$VEPATH = Replace($VEPATH,"/","\")
  	$VELOOKUP = '=IfERROR(VLOOKUP($$B$EXCELROW8,BACKEND!$$A:$$B,2,FALSE),"")'
  	$ARRAYS = $VEARRAYS[2],$VEPATH,$VELOOKUP
  	$oXL.Range("A$EXCELROW8:C$EXCELROW8").Value = $ARRAYS
  	$EXCELROW8 = $EXCELROW8 + 1
  	$VEDETAILS = ReadLine (7)
  	$VEDETAILS = Replace($VEDETAILS,'"',"")
  Loop
	
	Close (7)
Next

$oXL.Range("A1:C1").EntireColumn.AutoFit 	


All this works fine. However as time goes on we are getting many duplicates from the exports, their nature, and since I use the exports as VLOOKUP references I want to trim out the dups to speed up the searches.

This is where I tried importing the VB command REMOVEDUPLICATES.

And so here I am. Again the test data can be a small excel sheet with 3 or 4, 5 items with at least one repeating:

APPLE
PEAR
FRUIT
TWO
PEAR


It's the porting of the REMOVEDUPLICATES method from VB basic to Kix I can't get right.

Please let me know if you need more info.
Thanks


Edited by 99osou (2012-08-30 08:09 PM)

Top
#205617 - 2012-08-31 03:03 PM Re: REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix [Re: 99osou]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
I was sort of hoping for a bare bones structure. After a little reading it appears that Removeduplicates is available to Excel 2007 and above.

First I created a simple spreadsheet with your sample data, saved and closed. Then I fired up Excel and told it to record a macro opening the spreadsheet and removing dupes. I then stopped the recording, and then viewed the code... which converted to kix is below:

 Code:
$oXL = CreateObject("EXCEL.application")
$OXL.visible=1
$RC=$oXL.workbooks.Open("D:\Excel\RemoveDuplicates\test.xlsx")
$oXL.ActiveSheet.Range("$A$1:$A$5").RemoveDuplicates(1,0)


Does this help?

Top
#205618 - 2012-08-31 03:20 PM Re: REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix [Re: Allen]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
LOL That simple? Just 4 lines?
Works great.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#205619 - 2012-08-31 04:39 PM Re: REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix [Re: 99osou]
99osou Offline
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
 Code:
$oXL.Range("A1:C1").EntireColumn.AutoFit


writing a data set to an excel range:
 Code:
$ARRAYS = UCase($BKNASSOURCE),UCase($BKNASTRUNCSOURCE),UCase($NASARRAY[5])
$oXL.Range("A1:C1").Value = $ARRAYS


an excel sort routine
 Code:
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

 Code:
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
 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
 Code:
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

 Code:
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
#205643 - 2012-09-01 02:24 AM Re: REMOVEDUPLICATE Method For Excel Via Com Scripting in Kix [Re: 99osou]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11627
Loc: CA
Thank you for sharing. Always good to have around for that "some day" when the Boss is in a hurry to get something from you.
Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 456 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.07 seconds in which 0.037 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org