VBA macro to extra unique values in column

Posted on 2012-09-18
Medium Priority
Last Modified: 2012-09-20
Dear experts,

Could any one please provide me a vba query which can extract the unique values in a column.

The vba will be in the file on which the query has to be run.

the vba should be enable for prompting for selection of sheet and column.

Thank you
Question by:Excellearner
  • 2
  • 2
LVL 10

Expert Comment

ID: 38411827
Try this:
Sub test()
  'Dim wsList() As Strings
  Dim i As Long
  Dim wsname As String
  Dim col As String
  Dim ncol As Integer
  Dim c As Range
  Dim R As Range
  Dim result As String
  wsname = InputBox("Please enter the sheet name", "Sheet Name")
  col = InputBox("Please enter the Column name or number", "Column Name")
  If IsNumeric(col) Then
    Set R = Sheets(wsname).Columns(CInt(col))
    Set R = Sheets(wsname).Columns(col)
  End If
  i = R.Rows(R.Rows.Count).End(xlUp).Row
  For Each c In Range(R.Rows(1), R.Rows(i))
    If WorksheetFunction.CountIfs(R, "=" & c.Value) = 1 Then
        result = result & c.Value & vbCrLf
    End If
  Next c
  MsgBox result
End Sub

Author Comment

ID: 38413091

Thank you for the comment,

The Macro (MSwindows 2007 with Excel 2007 version) gave error for the below code ' as not supported by vba'

  'Dim wsList() As Strings

Kindly help.
LVL 10

Accepted Solution

SANTABABY earned 2000 total points
ID: 38414815
The workbook is updated with code in (Module1). The macro can be troiggered by Ctrl+Shift+u
The results are written in "UniqueValues" worksheet in the workbook.
To Test:
1. Open the workbook
2. Press Ctrl+Shift+u

Author Closing Comment

ID: 38416959
The macro is giving the expected result.

Thank you very much

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question