Filter unique values

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
This VBA code returns the unique values from column A of the CURRENT tab. How could I also get the results of column B that go with the results from column A? I would like to put those results in Cells(2, 5).

VBA Code:
Sub FilterLocation()
    
    Dim Dn As Range
    Dim rng As Range
    Dim ws  As Worksheet
    Set ws = Sheets("CURRENT")
    Set rng = ws.Range("A2", ws.Range("A" & ws.Rows.Count).End(xlUp))
    
    With CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare
        For Each Dn In rng
            If Not .Exists(Dn.Value) _
                And Dn.Offset(, 3).Value = Sheets("Sheet2").Range("B2").Value _
                And Dn.Offset(, 4).Value = Sheets("Sheet2").Range("B3").Value Then
                .Add Dn.Value, ""
            End If
        Next
        Sheets("Sheet2").Cells(2, 6).Resize(.Count).Value = Application.Transpose(.keys)
    End With
    
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
How about
Rich (BB code):
Sub FilterLocation()
    
    Dim Dn As Range
    Dim rng As Range
    Dim ws  As Worksheet
    Set ws = Sheets("CURRENT")
    Set rng = ws.Range("A2", ws.Range("A" & ws.Rows.Count).End(xlUp))
    
    With CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare
        For Each Dn In rng
            If Not .Exists(Dn.Value) _
                And Dn.Offset(, 3).Value = Sheets("Sheet2").Range("B2").Value _
                And Dn.Offset(, 4).Value = Sheets("Sheet2").Range("B3").Value Then
                .Add Dn.Value, Dn.Offset(, 1).Value
            End If
        Next
        Sheets("Sheet2").Cells(2, 5).Resize(.Count, 2).Value = Application.Transpose(Array(.Items, .Keys))
    End With
    
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
H Fluff, I may have jumped the gun. I actually would like to return one other column. Ultimately, columns A, B, and C from the CURRENT tab and returned to D, E, and F on Sheet2

I added

VBA Code:
.Add Dn.Value, Dn.Offset(, 1).Value, Dn.Offset(, 2).value

But this below I didn't know what to do with.

VBA Code:
Sheets("Sheet2").Cells(2, 5).Resize(.Count, 2).Value = Application.Transpose(Array(.Items, .Keys))

Other then change to Cells(2, 4) and then (.Count, 3)
 
Upvote 0
columns A, B, and C from the CURRENT tab and returned to D, E, and F on Sheet2
Try this modification (assuming that semicolons are not being used in cols B:C of 'CURRENT')
VBA Code:
Sub FilterLocation()
   
    Dim Dn As Range
    Dim rng As Range
    Dim ws  As Worksheet
    Set ws = Sheets("CURRENT")
    Set rng = ws.Range("A2", ws.Range("A" & ws.Rows.Count).End(xlUp))
   
    With CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare
        For Each Dn In rng
            If Not .Exists(Dn.Value) _
                And Dn.Offset(, 3).Value = Sheets("Sheet2").Range("B2").Value _
                And Dn.Offset(, 4).Value = Sheets("Sheet2").Range("B3").Value Then
                .Add Dn.Value, Dn.Offset(, 1).Value & ";" & Dn.Offset(, 2).Value
            End If
        Next

        Sheets("Sheet2").Cells(2, 5).Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
        With Sheets("Sheet2")
          .Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp)).TextToColumns DataType:=xlDelimited, Semicolon:=True, Comma:=False, Space:=False, Other:=False
        End With
         
    End With
   
End Sub
 
Upvote 0
That's amazing Peter and it works grand. Thanks you.
 
Upvote 0
You're welcome. Thanks for the confirmation. :)
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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