Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get unique values from a list of values using VBscript?

Tags:

vbscript

Suppose an Excel sheet has a column named Student Names and the column has duplicate values. Say,

Student
=======

Arup
John
Mike
John
Lisa
Arup

Using VBScript, how can I get unique values as below?

Arup
John
Lisa
Mike
like image 980
Arup Rakshit Avatar asked Dec 06 '12 11:12

Arup Rakshit


1 Answers

The VBScript tool for getting unique items is a dictionary: add all the items as keys to a dictionary and dictionary.Keys() will return an array of the - per definitionem - unique keys. In code:

  Dim aStudents : aStudents = Array("Arup", "John", "Mike", "John", "Lisa", "Arup")
  WScript.Echo Join(aStudents)
  Dim aUniqStudents : aUniqStudents = uniqFE(aStudents)
  WScript.Echo Join(aUniqStudents)

' returns an array of the unique items in for-each-able collection fex
Function uniqFE(fex)
  Dim dicTemp : Set dicTemp = CreateObject("Scripting.Dictionary")
  Dim xItem
  For Each xItem In fex
      dicTemp(xItem) = 0
  Next
  uniqFE = dicTemp.Keys()
End Function

output:

Arup John Mike John Lisa Arup
Arup John Mike Lisa
like image 110
Ekkehard.Horner Avatar answered Sep 30 '22 15:09

Ekkehard.Horner