VBA to extract unique values from different sheets and different columns

amandeep08

Board Regular
Joined
Mar 20, 2011
Messages
130
Office Version
  1. 365
Dear All,

I have one excel workbook with 3 different sheets. I want the unique values from these sheets but the problem is that the data is in different columns. How i can extract the Unique name in new sheet. Attached is the sample file

Book1
D
8
Summary


Aman
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Attached is the sample file
You need to actually select the range that you want to show before clicking Mini sheet.

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Dear All,

I have one excel workbook with 3 different sheets. I want the unique values from these sheets but the problem is that the data is in different columns. How i can extract the Unique name in new sheet. Attached is the sample file

Book1
D
8
Summary


Aman
Sample File.xlsx
ABC
1Unique Name
2ASD
3SDF
4DFG
5FGH
6ter
7wert
8hrrd
9yyu
10tyr
11
Summary
 
Upvote 0
The above single mini sheet does show us anything about this ..
3 different sheets. I want the unique values from these sheets but the problem is that the data is in different columns.

(You also have not addressed the last paragraph of my previous post)
 
Upvote 0
See if this is any use to you:-
(it assumes that each sheet has a column called "Name", change that if it is called something else)

VBA Code:
Sub UniqueNames()

    Dim srcSht As Worksheet, sumSht As Worksheet
    Dim strHdg As String, rFindHdg As Range, srcRng As Range
    Dim dictSrc As Object, dictKey As String
    Dim srcArr As Variant
    Dim i As Long
    
    Set sumSht = Worksheets("Summary")              '<--- Change if required
    strHdg = "Name"                                 '<--- Change if required
    Set dictSrc = CreateObject("Scripting.dictionary")
    
    For Each srcSht In Worksheets(Array("Sheet1", "Sheet2", "Sheet3")) '<--- Change the names of the 3 sheets
        
        With srcSht
            Set rFindHdg = srcSht.UsedRange.Find(What:=strHdg, LookIn:=xlValues, _
                                 LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                 MatchCase:=False, SearchFormat:=False)
            If rFindHdg Is Nothing Then Exit For
            
            srcArr = .Range(rFindHdg.Offset(1), .Cells(Rows.Count, rFindHdg.Column).End(xlUp))
        End With
        ' Load names column in each sheet into Dictionary
        For i = 1 To UBound(srcArr)
            dictKey = srcArr(i, 1)
            If Not dictSrc.exists(dictKey) Then
                dictSrc(dictKey) = i
            End If
        Next i
    Next srcSht
    
    With sumSht
        .Range("A1").CurrentRegion.Offset(1).ClearContents
        .Range("A1").Offset(1).Resize(dictSrc.Count) = Application.Transpose(dictSrc.Keys)
        .Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
    End With

End Sub
 
Upvote 0
H
See if this is any use to you:-
(it assumes that each sheet has a column called "Name", change that if it is called something else)

VBA Code:
Sub UniqueNames()

    Dim srcSht As Worksheet, sumSht As Worksheet
    Dim strHdg As String, rFindHdg As Range, srcRng As Range
    Dim dictSrc As Object, dictKey As String
    Dim srcArr As Variant
    Dim i As Long
   
    Set sumSht = Worksheets("Summary")              '<--- Change if required
    strHdg = "Name"                                 '<--- Change if required
    Set dictSrc = CreateObject("Scripting.dictionary")
   
    For Each srcSht In Worksheets(Array("Sheet1", "Sheet2", "Sheet3")) '<--- Change the names of the 3 sheets
       
        With srcSht
            Set rFindHdg = srcSht.UsedRange.Find(What:=strHdg, LookIn:=xlValues, _
                                 LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                 MatchCase:=False, SearchFormat:=False)
            If rFindHdg Is Nothing Then Exit For
           
            srcArr = .Range(rFindHdg.Offset(1), .Cells(Rows.Count, rFindHdg.Column).End(xlUp))
        End With
        ' Load names column in each sheet into Dictionary
        For i = 1 To UBound(srcArr)
            dictKey = srcArr(i, 1)
            If Not dictSrc.exists(dictKey) Then
                dictSrc(dictKey) = i
            End If
        Next i
    Next srcSht
   
    With sumSht
        .Range("A1").CurrentRegion.Offset(1).ClearContents
        .Range("A1").Offset(1).Resize(dictSrc.Count) = Application.Transpose(dictSrc.Keys)
        .Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
    End With

End Sub
Hi Alex,

Thanks a lot for the Coding.

It is providing the results as per expectation.

I will thoroughly check the same.

Aman
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top