Extract Unique Values from Multiple Sheets

ravi2628

Board Regular
Joined
Dec 20, 2017
Messages
221
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Friends,

Good Morning, Afternoon, Evening, Night.

I required a VBA code or Formula to Extract the unique values from Multiple Sheets and I need to specify the column manually.

Input:
Sheet1:
Book1
ABC
1CountryGeographyMolecule description
2SrilankaNon LatamAbiraterone Acetate Tablets 250 mg
3BhutanNon LatamAdenosine injection USP 6mg/2mL
4PeruLatamAdenosine injection USP 6mg/2mL
5BhutanNon LatamAmphotericin B liposome injection 50mg
6El SalvadorLatamAmphotericin B liposome injection 50mg
7HondurasLatamAmphotericin B liposome injection 50mg
8ParaguayLatamAmphotericin B liposome injection 50mg
9PeruLatamAmphotericin B liposome injection 50mg
10SyriaNon LatamAmphotericin B liposome injection 50mg
11GuatemalaLatamAnastrazole tablets IP 1mg
12HondurasLatamAnastrazole tablets IP 1mg
13NepalNon LatamAnastrazole Tablets IP 1mg
14SrilankaNon LatamAnastrazole tablets IP 1mg
15Trinidad & TobagoLatamAnastrazole tablets IP 1mg
16BhutanNon LatamAtracurium Besylate injection 25mg
17El SalvadorLatamBicalutamide Tablets USP 50mg
18GuatemalaLatamBicalutamide Tablets USP 50mg
19HondurasLatamBicalutamide Tablets USP 50mg
20PhilippinesNon LatamBicalutamide Tablets USP 50mg
21Dominican RepublicLatamBleomycin for Inj 15 units USP
22DR CongoNon LatamBleomycin for Inj 15 units USP
23El SalvadorLatamBleomycin for Inj 15 units USP
24GuatemalaLatamBleomycin for Inj 15 units USP
25HondurasLatamBleomycin for Inj 15 units USP
26NicaraguaLatamBleomycin for Inj 15 units USP
27SyriaNon LatamBleomycin for Inj 15 units USP
28HondurasLatamBortezomib for injection 2mg
29NepalNon LatamBortezomib for Injection 2mg
30SrilankaNon LatamBortezomib for injection 2mg
31ChileLatamBortezomib for injection 3.5 mg
32Dominican RepublicLatamBortezomib for injection 3.5 mg
33GuatemalaLatamBortezomib for injection 3.5 mg
34SyriaNon LatamBortezomib for injection 3.5 mg
35Trinidad & TobagoLatamBortezomib for injection 3.5 mg
36NicaraguaLatamBupivacaine Hcl Injection 50mg/20ml
37PhilippinesNon LatamBupivacaine Hcl in Dextrose Inj 20mg
38PeruLatamBupivacaine Hcl injection 100mg/20ml
39JamaicaLatamBusulfan Injection 60mg
40ColombiaLatamBusulfan Injection 60mg(VIAL)
41GuatemalaLatamBusulfan Injection 60mg(VIAL)
42HondurasLatamBusulfan Injection 60mg(VIAL)
43IranNon LatamBusulfan Injection 60mg(VIAL)
44PeruLatamBusulfan Injection 60mg(VIAL)
45SyriaNon LatamBusulfan Injection 60mg(VIAL)
46ChileLatamBusulfan tablets 2mg
47GuatemalaLatamBusulfan tablets 2mg
48PhilippinesNon LatamBusulfan tablets 2mg
49DR CongoNon LatamCapecitabine tablets USP 500mg
Registration


Sheet2:
Book1
ABC
1CountryGeographyMolecule description
2EcuadorLatamFludarabine Phosphate for inj USP 50 mg
3GeorgiaNon LatamCyclophosphamide injection IP 200
4GeorgiaNon LatamCyclophosphamide injection IP 500
5GeorgiaNon LatamCyclophosphamide injection IP 1000
6GeorgiaNon LatamIrinotecan Hcl Injection IP 40mg/2mL
7GeorgiaNon LatamIrinotecan Hcl Injection IP 100mg/5mL
8GeorgiaNon LatamFludarabine Phosphate for inj USP 50 mg
9GeorgiaNon LatamHydroxy urea capsules USP 500mg
10GeorgiaNon LatamEpirubicin Hcl for Injection USP 100 mg
11GeorgiaNon LatamOndansetron Injection USP 4mg/2mL
12GeorgiaNon LatamOndansetron Hcl injection 8mg/4mL
13GeorgiaNon LatamCapecitabine tablets USP 500mg
14GeorgiaNon LatamCarboplatin Injection BP 150mg/15mL
15GeorgiaNon LatamCarboplatin Injection BP 450mg/45mL
16GeorgiaNon LatamDoxorubicin Hydrochloride for Injection USP 10mg
17GeorgiaNon LatamDoxorubicin Hcl for injection USP 50mg
18GeorgiaNon LatamFluorouracil injection IP/USP 250mg/5mL
19NigeriaNon LatamCarboplatin Injection BP 450mg/45mL
20NigeriaNon LatamCisplatin injection BP 50mg/50mL
21NigeriaNon LatamDoxorubicin Hcl for injection USP 50mg
22NigeriaNon LatamGemcitabine for injection USP 1000 mg
23NigeriaNon LatamGemcitabine for injection USP 200 mg
24NigeriaNon LatamOxaliplatin for Injection 100 mg
25PeruLatamDoxorubicin Hydrochloride for Injection USP 10mg
26PeruLatamDoxorubicin Hcl for injection USP 50mg
27PeruLatamBleomycin for Inj 15 units USP
28PeruLatamEpirubicin Hcl for Injection USP 50 mg
29PeruLatamFludarabine Phosphate for inj USP 50 mg
30PeruLatamGemcitabine for injection USP 1000 mg
31PeruLatamVincristine sulphate for inj USP/IP 1mg
32PeruLatamZoledronic acid for injection 4mg
33PeruLatamDacarbazine for injection USP 200mg
34PeruLatamDactinomycin for Injection USP 0.5mg
35PeruLatamOxaliplatin for Injection 100 mg
36PeruLatamImatinib tablets 400mg
37PeruLatamOndansetron Hcl injection 8mg/4mL
38SrilankaNon LatamMelphalan Tablets IP/USP 2mg
39SyriaNon LatamCyclophosphamide injection IP 1000
40SyriaNon LatamDoxorubicin Hcl for injection USP 50mg
41SyriaNon LatamDoxorubicin Hydrochloride for Injection USP 10mg
42SyriaNon LatamCytarabine injection IP/BP 100mg/mL
43SyriaNon LatamCytarabine injection IP/BP 1000mg/10mL
44UzbekistanNon LatamTamoxifen citrate Tablets IP/USP 10mg
45UzbekistanNon LatamZoledronic acid for injection 4mg
46KenyaNon LatamPropofol injection BP/USP 200mg/20ml
47KenyaNon LatamHeparin Sodium Injection 5000 IU
Renewal


Sheet3:
Book1
ABC
1CountryGeographyMolecule description
2BhutanNon LatamBupivacaine Hcl injection 100mg/20ml
3BhutanNon LatamNeostigmine Methylsulfate inj 2.5mg/5ml
4BhutanNon LatamEnoxaparin Sodium injection 60mg
5Costarica(CCSS)LatamSuccinylchloine Injection USP 500 mg/10 mL
6DR CongoNon LatamBicalutamide Tablets USP 50mg
7DR CongoNon LatamEnoxaparin Sodium injection 40mg
8DR CongoNon LatamEnoxaparin Sodium injection 60mg
9DR CongoNon LatamFluorouracil injection IP/USP 250mg/5mL
10DR CongoNon LatamVincristine sulphate for inj USP/IP 1mg
11DR CongoNon LatamOxaliplatin for Injection USP 50 mg
12DR CongoNon LatamMethotrexate Injection IP/USP 1000mg
13EcuadorLatamDoxorubicin Hydrochloride for Injection USP 10mg
14EcuadorLatamDoxorubicin Hcl for injection USP 50mg
15EcuadorLatamThalidomide Capsules USP 100 mg
16EcuadorLatamL-Asparaginase for Injection 10000IU
17GeorgiaNon LatamAmphotericin B liposome injection 50mg
18GeorgiaNon LatamMelphalan Tablets IP/USP 2mg
19GeorgiaNon LatamMelphalan Tablets USP 5mg
20GeorgiaNon LatamChlormabucil tablets IP/USP 2mg
21GeorgiaNon LatamChlormabucil tablets IP/USP 5mg
22GeorgiaNon LatamLenalidomide Capsules 10 mg
23GeorgiaNon LatamLenalidomide Capsules 25 mg
24GeorgiaNon LatamAbiraterone Acetate Tablets 250 mg
25GeorgiaNon LatamCarmustine for injection USP 100mg
26GeorgiaNon LatamBusulfan Injection 60mg(VIAL)
27IranNon LatamChlormabucil tablets IP/USP 2mg
28IranNon LatamFluorouracil injection IP/USP 250mg/5mL
29IranNon LatamFluorouracil injection IP/USP500mg/10mL
30JamaicaLatamAcetylcystine Injection BP 200mg/ml
31JamaicaLatamAmphotericin B liposome injection 50mg
32JamaicaLatamBicalutamide Tablets USP 50mg
33JamaicaLatamBupivacaine Hcl Injection 50mg/20ml
34JamaicaLatamCapecitabine tablets USP 500mg
35JamaicaLatamHeparin Sodium IP/USP 25000IU/5mL
36JamaicaLatamL-Asparaginase for Injection 10000IU (New Brand)
37JamaicaLatamMelphalan injection IP/ BP 50mg
38JamaicaLatamMethotrexate injection 50mg/2ml(amp)
39JamaicaLatamsterile nor-adreline conc inj 4mg/2mL
40JamaicaLatamPolymyxin B Sulphate for inj 500000 IU
41JamaicaLatamTranexamic acid injection BP 500mg
42JamaicaLatamVancomycin Hcl for inj IP/USP 1000mg
43KenyaNon LatamCapecitabine tablets USP 500mg
44KenyaNon LatamBortezomib for injection 3.5 mg
45KenyaNon LatamCarboplatin Injection BP 450mg/45mL
46KenyaNon LatamOxaliplatin for Injection USP 50 mg
47KenyaNon LatamZoledronic acid for injection 4mg
48KenyaNon LatamDoxorubicin Hcl Liposomal Injection 50mg/25mL
49KenyaNon LatamPaclitaxel NAB Inj. Suspension 100mg
50KenyaNon LatamDacarbazine for injection USP 200mg
New Registration


Output Required is :

one column is based on column A and another column is based on column C. So my columns may vary based on time so I need to select the column and specific Column where data has been filled.

Book1
ABCDEF
1CountryMolecule description
2BhutanAbiraterone Acetate Tablets 250 mg
3ChileAcetylcystine Injection BP 200mg/ml
4ColombiaAdenosine injection USP 6mg/2mL
5Costarica(CCSS)Amphotericin B liposome injection 50mg
6Dominican RepublicAnastrazole tablets IP 1mg
7DR CongoAtracurium Besylate injection 25mg
8EcuadorBicalutamide Tablets USP 50mg
9El SalvadorBleomycin for Inj 15 units USP
10GeorgiaBortezomib for injection 2mg
11GuatemalaBortezomib for injection 3.5 mg
12HondurasBupivacaine Hcl in Dextrose Inj 20mg
13IranBupivacaine Hcl injection 100mg/20ml
14JamaicaBupivacaine Hcl Injection 50mg/20ml
15KenyaBusulfan Injection 60mg
16NepalBusulfan Injection 60mg(VIAL)
17NicaraguaBusulfan tablets 2mg
18NigeriaCapecitabine tablets USP 500mg
19ParaguayCarboplatin Injection BP 150mg/15mL
20PeruCarboplatin Injection BP 450mg/45mL
21PhilippinesCarmustine for injection USP 100mg
22SrilankaChlormabucil tablets IP/USP 2mg
23SyriaChlormabucil tablets IP/USP 5mg
24Trinidad & TobagoCisplatin injection BP 50mg/50mL
25UzbekistanCyclophosphamide injection IP 1000
26Cyclophosphamide injection IP 200
27Cyclophosphamide injection IP 500
28Cytarabine injection IP/BP 1000mg/10mL
29Cytarabine injection IP/BP 100mg/mL
30Dacarbazine for injection USP 200mg
31Dactinomycin for Injection USP 0.5mg
32Doxorubicin Hcl for injection USP 50mg
33Doxorubicin Hcl Liposomal Injection 50mg/25mL
34Doxorubicin Hydrochloride for Injection USP 10mg
35Enoxaparin Sodium injection 40mg
36Enoxaparin Sodium injection 60mg
37Epirubicin Hcl for Injection USP 100 mg
38Epirubicin Hcl for Injection USP 50 mg
39Fludarabine Phosphate for inj USP 50 mg
40Fluorouracil injection IP/USP 250mg/5mL
41Fluorouracil injection IP/USP500mg/10mL
42Gemcitabine for injection USP 1000 mg
43Gemcitabine for injection USP 200 mg
44Heparin Sodium Injection 5000 IU
45Heparin Sodium IP/USP 25000IU/5mL
46Hydroxy urea capsules USP 500mg
47Imatinib tablets 400mg
48Irinotecan Hcl Injection IP 100mg/5mL
49Irinotecan Hcl Injection IP 40mg/2mL
50L-Asparaginase for Injection 10000IU
51L-Asparaginase for Injection 10000IU (New Brand)
52Lenalidomide Capsules 10 mg
53Lenalidomide Capsules 25 mg
54Melphalan injection IP/ BP 50mg
55Melphalan Tablets IP/USP 2mg
56Melphalan Tablets USP 5mg
57Methotrexate injection 50mg/2ml(amp)
58Methotrexate Injection IP/USP 1000mg
59Neostigmine Methylsulfate inj 2.5mg/5ml
60Ondansetron Hcl injection 8mg/4mL
61Ondansetron Injection USP 4mg/2mL
62Oxaliplatin for Injection 100 mg
63Oxaliplatin for Injection USP 50 mg
64Paclitaxel NAB Inj. Suspension 100mg
65Polymyxin B Sulphate for inj 500000 IU
66Propofol injection BP/USP 200mg/20ml
67sterile nor-adreline conc inj 4mg/2mL
68Succinylchloine Injection USP 500 mg/10 mL
69Tamoxifen citrate Tablets IP/USP 10mg
70Thalidomide Capsules USP 100 mg
71Tranexamic acid injection BP 500mg
72Vancomycin Hcl for inj IP/USP 1000mg
73Vincristine sulphate for inj USP/IP 1mg
74Zoledronic acid for injection 4mg
Unique value



Thanks in advance.

Regards,
Ravi
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Where you want your out located, same or new workbook?
 
Upvote 0
Try this
VBA Code:
Option Compare Text

Sub GetUnique()

Dim strCombo As String, col1 As String, col2 As String
Dim nRow As Long
Dim key As Variant
Dim cell As Range, rngData As Range
Dim ws As Worksheet, wsU As Worksheet
Dim wb As Workbook
Dim dict As Object

Set wb = ActiveWorkbook
Set wsU = wb.Sheets("Unique Value")
Set dict = CreateObject("Scripting.Dictionary")

For Each ws In wb.Sheets
    If Not ws.Name = "Unique Value" Then
        Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
        For Each cell In rngData
            strCombo = ws.Range("A" & cell.Row) & "," & ws.Range("C" & cell.Row)
            If Not dict.Exists(strCombo) Then
                dict.Add strCombo, Nothing
            End If
        Next
    End If
Next

col1 = Application.InputBox("Please enter Country column")
col2 = Application.InputBox("Please enter Molecule Description column")

nRow = 1
For Each key In dict
    nRow = nRow + 1
    With wsU
        .Range(col1 & nRow) = Split(key, ",")(0)
        .Range(col2 & nRow) = Split(key, ",")(1)
    End With
Next

End Sub
 
Upvote 0
Try this
VBA Code:
Option Compare Text

Sub GetUnique()

Dim strCombo As String, col1 As String, col2 As String
Dim nRow As Long
Dim key As Variant
Dim cell As Range, rngData As Range
Dim ws As Worksheet, wsU As Worksheet
Dim wb As Workbook
Dim dict As Object

Set wb = ActiveWorkbook
Set wsU = wb.Sheets("Unique Value")
Set dict = CreateObject("Scripting.Dictionary")

For Each ws In wb.Sheets
    If Not ws.Name = "Unique Value" Then
        Set rngData = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
        For Each cell In rngData
            strCombo = ws.Range("A" & cell.Row) & "," & ws.Range("C" & cell.Row)
            If Not dict.Exists(strCombo) Then
                dict.Add strCombo, Nothing
            End If
        Next
    End If
Next

col1 = Application.InputBox("Please enter Country column")
col2 = Application.InputBox("Please enter Molecule Description column")

nRow = 1
For Each key In dict
    nRow = nRow + 1
    With wsU
        .Range(col1 & nRow) = Split(key, ",")(0)
        .Range(col2 & nRow) = Split(key, ",")(1)
    End With
Next

End Sub
Hi Zot,

When I was Using the Code I was Getting Duplicate Values in the data

and from hidden sheets also data is being taken by the macro.

For your reference, I was sharing a snapshot.
 

Attachments

  • Unique Values From Sheets.JPG
    Unique Values From Sheets.JPG
    134.3 KB · Views: 19
Upvote 0
I guess I did not understand your requirement fully. I thought you were talking about unique combination of Country and Molecule Description. I seems like you want them separately, unique Country list and unique Molecule Description list. Is this right?

If that is so then try this
VBA Code:
Option Compare Text

Sub GetUnique()

Dim strCombo As String, col1 As String, col2 As String
Dim nRow As Long
Dim key As Variant
Dim cellC As Range, rngC As Range
Dim cellMD As Range, rngMD As Range
Dim ws As Worksheet, wsU As Worksheet
Dim wb As Workbook
Dim dictC As Object, dictMD As Object

Set wb = ActiveWorkbook
Set wsU = wb.Sheets("Unique Value")
Set dictC = CreateObject("Scripting.Dictionary")
Set dictMD = CreateObject("Scripting.Dictionary")

For Each ws In wb.Sheets
    If Not ws.Name = "Unique Value" Then
        Set rngC = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
        Set rngMD = ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
        For Each cellC In rngC
            If Not dictC.Exists(cellC.Value) Then
                dictC.Add cellC.Value, Nothing
            End If
        Next
        For Each cellMD In rngMD
            If Not dictMD.Exists(cellMD.Value) Then
                dictMD.Add cellMD.Value, Nothing
            End If
        Next
    End If
Next

col1 = Application.InputBox("Please enter Country column")
col2 = Application.InputBox("Please enter Molecule Description column")

nRow = 1
For Each key In dictC
    nRow = nRow + 1
    With wsU
        .Range(col1 & nRow) = key
    End With
Next
nRow = 1
For Each key In dictMD
    nRow = nRow + 1
    With wsU
        .Range(col2 & nRow) = key
    End With
Next

End Sub
 
Last edited:
Upvote 0
Solution
I guess I did not understand your requirement fully. I thought you were talking about unique combination of Country and Molecule Description. I seems like you want them separately, unique Country list and unique Molecule Description list. Is this right?

If that is so then try this
VBA Code:
Option Compare Text

Sub GetUnique()

Dim strCombo As String, col1 As String, col2 As String
Dim nRow As Long
Dim key As Variant
Dim cellC As Range, rngC As Range
Dim cellMD As Range, rngMD As Range
Dim ws As Worksheet, wsU As Worksheet
Dim wb As Workbook
Dim dictC As Object, dictMD As Object

Set wb = ActiveWorkbook
Set wsU = wb.Sheets("Unique Value")
Set dictC = CreateObject("Scripting.Dictionary")
Set dictMD = CreateObject("Scripting.Dictionary")

For Each ws In wb.Sheets
    If Not ws.Name = "Unique Value" Then
        Set rngC = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
        Set rngMD = ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))
        For Each cellC In rngC
            If Not dictC.Exists(cellC.Value) Then
                dictC.Add cellC.Value, Nothing
            End If
        Next
        For Each cellMD In rngMD
            If Not dictMD.Exists(cellMD.Value) Then
                dictMD.Add cellMD.Value, Nothing
            End If
        Next
    End If
Next

col1 = Application.InputBox("Please enter Country column")
col2 = Application.InputBox("Please enter Molecule Description column")

nRow = 1
For Each key In dictC
    nRow = nRow + 1
    With wsU
        .Range(col1 & nRow) = key
    End With
Next
nRow = 1
For Each key In dictMD
    nRow = nRow + 1
    With wsU
        .Range(col2 & nRow) = key
    End With
Next

End Sub
The new code is working fine but it is taking hidden sheet cell values also so please help me with that.
 
Upvote 0
Can it be possible to Select only Visible worksheets in the macro?
 
Upvote 0
Can it be possible to Select only Visible worksheets in the macro?

Add this condition
Rich (BB code):
For Each ws In wb.Sheets
    If Not ws.Name = "Unique Value" And ws.Visible = xlSheetVisible Then
        Set rngC = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
 
Upvote 0
Add this condition
Rich (BB code):
For Each ws In wb.Sheets
    If Not ws.Name = "Unique Value" And ws.Visible = xlSheetVisible Then
        Set rngC = ws.Range("A2", ws.Cells(Rows.Count, "A").End(xlUp))
Thanks Zot Your logic helped me a lot
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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