FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

Excel Tutorial: Finding unique values in a column and paste it in another using MS Excel Macro

jawahar
2/15/2007 3:50:01 PM, Views: 1122
The following tutorial and code can be used to fetch values from a column and paste it into another using MS Excel Macro.

Open MS Excel and Select Tools->Macro->Visual Basic Editor

In the Left side Project Window:
Right Click-> Insert module

Paste the following code:

Public Function UniqueValues(ByVal SourceValues As Range, ByVal DestColumn As String, ByVal DestRowNum As Integer) As Variant
'Returns a variant containing the unique values in a range
'If called from a worksheet array formula, returns either a row or column array, as needed.
Dim Items As New Collection
Dim i As Long, m As Long, nCols As Long, nRows As Long, Row As Long
Dim cel As Range
Dim Result() As Variant
Dim cel1 As Range
Dim SV As Range
Dim RV As Range
Dim cel2 As Range
Dim mino As Integer

On Error Resume Next
For Each cel In SourceValues.Cells
    If cel <> "" Then Items.Add cel, CStr(cel)
Next
nCols = Application.Caller.Columns.Count
nRows = Application.Caller.Rows.Count
m = Application.Max(nCols, nRows)
On Error GoTo 0

i = Items.Count
ReDim Result(1 To i)
For Row = 1 To i
    Result(Row) = Items(Row)
Next Row

If m > i Then
      ReDim Preserve Result(1 To m)
      For Row = i + 1 To m
          Result(Row) = ""
      Next Row
End If
      For Row = 1 To i
          ActiveSheet.Range(DestColumn & DestRowNum + (Row - 1)) = Result(Row)
      Next Row
End Function


Once again In the Left side Project Window:
Right Click-> Insert module

Paste the following Code:

Sub UseIt()
      Application.ScreenUpdating = False
      'UniqueValues(Source Column Range, Destination Column Name, Destination Column Row Number)
      Call UniqueValues(ThisWorkbook.Worksheets("Sheet1").Range("A2:A100"), "C", 5)
      Application.ScreenUpdating = True
End Sub

Click File->Close to close the Visual Basic Editor.
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Not Yet Rated!
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
Excel RSS Feed
Most Popular Tutorial
Most Popular Solution
Top Rated
Top Rankers
Overall
1. jawahar (450)
Yearly -2008
1. jawahar (50)
Expertsforge Sponsors
bnrtop