VBA to Extract unique values from multiple columns in to two column

SteynBS

Board Regular
Joined
Jun 27, 2022
Messages
104
Office Version
  1. 365
Platform
  1. Windows
I need unique values in 2 columns to be pasted into another sheet in 2 columns.

I used this one but it pastes the values into 1 column

Sub ManyColDupes()
Dim MyDict As Object, InputRange As Range, OutputCol As Range, z As Variant, c As Variant

Set MyDict = CreateObject("Scripting.Dictionary")
Set InputRange = Sheets("Sheet6").Range("D1:BS1,CC1")
Set OutputCol = Sheets("Sheet7").Range("X1")

On Error Resume Next
For Each c In InputRange
For Each z In Range(c, c.Offset(Rows.Count - 1).End(xlUp))
If z <> "" Then MyDict(CStr(z)) = 1
Next z
Next c

OutputCol.Resize(MyDict.Count, 1).Value = WorksheetFunction.Transpose(MyDict.keys)

End Sub

Example
Column D must be pasted into the other sheet in Column B, and Column E needs to be pasted in the other sheet in Column C

Vendor Name Site
Premier Site 1
Premier Site 2
Nativa X
Cosmic Fashion Site C
MC Site X
MC Site Y


1657113593143.png
 
now I need to copy column B:H
Hmmm... It's a few clumsy code, but i think it should have work. For filtering by unique rows in the BCDEF columns with BCDEFGH copying.
Come back here next time you need additional assistance. :)

VBA Code:
Sub FilterBy5ColumnsCopy7()
Dim d As Object
Dim j, r As Range, c As Range, i&
Dim c1$, c2$, c3$, c4$, c5$, c6$, c7$

    Set r = Sheets("Sheet6").Range("B1:B" & Cells(1, 2).End(4).Row)
    Set d = CreateObject("Scripting.Dictionary")
    With d
        For Each c In r
            c1 = c.Value
            c2 = c.Offset(, 1).Value
            c3 = c.Offset(, 2).Value
            c4 = c.Offset(, 3).Value
            c5 = c.Offset(, 4).Value
            c6 = c.Offset(, 5).Value
            c7 = c.Offset(, 6).Value
            j = c1 & c2 & c3 & c4 & c5
            .Item(j) = Array(c1, c2, c3, c4, c5, c6, c7) 
        Next c
    i = 1
        For Each j In .Keys
            Sheets("Sheet7").Cells(i, 2).Resize(, 7) = Array(d(j)(0), d(j)(1), d(j)(2), d(j)(3), d(j)(4), d(j)(5), d(j)(6))
            i = i + 1
        Next j
    End With
End Sub
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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