Microsoft Excel – Usage of `Remove Duplicates`

There isn’t much about remove duplicates in scripts dictionary. How to use this since it doesn’t seem to work specifying only the selected range after remove duplicates in Microsoft Excel

In windows/VBA version it also expects a column input

image

Here is 3 method to remove duplicates from a list in ASObjC

use framework "Foundation"
use scripting additions

(**
* [Remove duplicate values in a list, return non ordered list]
*)
its removeDuplicate1({1, 2, 3, 7, 5, 8, 7, 4, 3, 2, 7, 9, 8})
on removeDuplicate1(theList)
	set theArray to current application's NSArray's arrayWithArray:theList
	return ((current application's NSSet's setWithArray:theArray)'s allObjects()) as list
end removeDuplicate1

(**
* [Remove duplicate values in a list, return non ordered list]
*)
its removeDuplicate2({1, 2, 3, 7, 5, 8, 7, 4, 3, 2, 7, 9, 8})
on removeDuplicate2(theList)
	set theArray to current application's NSArray's arrayWithArray:theList
	return (theArray's valueForKeyPath:"@distinctUnionOfObjects.self") as list
end removeDuplicate2

(**
* [Remove duplicate values in a list, return ordered list]
*)
its removeDuplicate3({1, 2, 3, 7, 5, 8, 7, 4, 3, 2, 7, 9, 8})
on removeDuplicate3(theList)
	set theArray to current application's NSArray's arrayWithArray:theList
	set orderedSet to current application's NSOrderedSet's orderedSetWithArray:theArray
	return orderedSet's array() as list
end removeDuplicate3

Its also possible to use: orderedSet’s allObjects() as list

I think the OP was asking about Word, though they didn’t say so.

Sorry that was on Microsoft Excel

Many AppleScript user use Script Libraries with useful methods to take inputs and return outputs. And many times its easier to use building block to build something we want. And it will be to try to understand how a scriptable application method works.

To get a range of cells from Excel as list, remove the duplicate, update the range.

Something like this, set values from A1 → A10 and the updated list will be stored in B1 → B10
You could set a count of the new list so get the index for B (count newList)

ex.

set value of range (“B1:B” & theCount) to newList

Performance: 2 ms for 10 values to remove duplicates

use framework "Foundation"
use scripting additions

tell application "Microsoft Excel"
	set theSheet to its active sheet
	tell theSheet
		set theValues to its value of range "A1:A10"
	end tell
end tell

set newList to its removeDuplicate(theValues)
set theCount to count of newList

tell application "Microsoft Excel"
	set theSheet to its active sheet
	tell theSheet
		-- set value of range "B1:B10" to newList
        set value of range ("B1:B" & theCount) to newList
	end tell
end tell

on removeDuplicate(theList)
	set theArray to current application's NSArray's arrayWithArray:theList
	set orderedSet to current application's NSOrderedSet's orderedSetWithArray:theArray
	return orderedSet's array() as list
end removeDuplicate

Works fine for me. You should include your code so that people can see what you’re doing.

This will fill in some data in a few cells and then remove the duplicate values. Finally, it returns a list of the updated, unique values.

tell application "Microsoft Excel"
	with timeout of 3 seconds
		
		-- enter data
		set sr to range ("A1:A7") -- initial range
		set vList to {{4}, {5}, {6}, {7}, {5}, {4}, {3}} -- initial data
		set value of sr to vList
		delay 2 -- allow time to view data
		
		remove duplicates sr
		
		-- get list of unique data
		set xList to {}
		set nr to range (get address of current region of cell "A1")
		set cnr to cells of nr
		
		repeat with vv from 1 to count of cnr
			set end of xList to (get value of item vv of cnr as integer)
		end repeat
		xList
		--> {4, 5, 6, 7, 3}
		
	end timeout
end tell

No this doesn’t reflect in the GUI of Excel