+ Reply to Thread
Results 1 to 11 of 11

VBA - Extracting unique data based on multiple criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    04-27-2017
    Location
    London
    MS-Off Ver
    MS Office 2013
    Posts
    38

    VBA - Extracting unique data based on multiple criteria

    Hello All,

    Hoping someone can kindly assist with this query.

    At present, I am using the following code for extracting unique data (but without any conditions):

    Sub CopyUnique()
        Dim s1 As Worksheet, s2 As Worksheet
        Set s1 = Sheets("Sheet2")
        Set s2 = Sheets("Sheet1")
        s1.Range("AB2:AB7000").Copy s2.Range("A3")
        s2.Range("C:C").RemoveDuplicates Columns:=1, Header:=xlNo
    End Sub
    What I'd also like to be able to do, is still extract the above but only when the values in Sheet1 Column A = Sheet2 $A$1 and Sheet 1 Column K = Sheet 2 $B$1.

    Hoping this is possible and that I've appropriately conveyed what I need.

    Thanks ever so much for the support.

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,292

    Re: VBA - Extracting unique data based on multiple criteria

    Hi donjamin

    Play around with this...assuming sheet1 has headers in row 1
    Option Explicit
    
    Sub Extract()
    Dim Val1 As String, Val2 As String
    Application.ScreenUpdating = False
    Val1 = Sheet2.Range("A1"): Val2 = Sheet2.Range("B1")
    With Sheet1
        With .Cells(1).CurrentRegion
            .AutoFilter 1, Val1
            .AutoFilter 11, Val2
            .Offset(1, 0).SpecialCells(12).Copy Sheet2.Range("A3")
        End With
        .AutoFilterMode = False
    End With
    Application.ScreenUpdating = True
    End Sub
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    04-27-2017
    Location
    London
    MS-Off Ver
    MS Office 2013
    Posts
    38

    Re: VBA - Extracting unique data based on multiple criteria

    Thanks sintek, much appreciate your support, however (and apologies) this isn't working and may be due to my lack of clarity.

    Where I noted it needed to match values in Sheet 1 Column A, it actually needs to match text, with Sheet 1 Column K being the value.

    Hope that helps.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,292

    Re: VBA - Extracting unique data based on multiple criteria

    Please upload a sample file...

  5. #5
    Registered User
    Join Date
    04-27-2017
    Location
    London
    MS-Off Ver
    MS Office 2013
    Posts
    38

    Re: VBA - Extracting unique data based on multiple criteria

    I'm struggling to figure out how to attach something as when I go to do it I'm just seeing a blank white box.

    Any ideas?

    I have managed to get this VBA to work, though without the desired result.

    My originally posted VBA would look at the entire workbook Column A - FJ and provide the unique values from column AC in the next sheet. I need the new one to just do that but with the conditions. The result of the VBA you've kindly provided is filtering as desired but only returning columns 1-12 when I just require column 29.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: VBA - Extracting unique data based on multiple criteria

    Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,292

    Re: VBA - Extracting unique data based on multiple criteria

    Option Explicit
    
    Sub Extract()
    Dim Val1 As String, Val2 As String, lr As Long
    Application.ScreenUpdating = False
    Val1 = Sheet2.Range("A1"): Val2 = Sheet2.Range("B1")
    With Sheet1
        With .Cells(1).CurrentRegion
            .AutoFilter 1, Val1
            .AutoFilter 11, Val2
        End With
        lr = .Range("AB" & Rows.Count).End(xlUp).Row
        .Range("AB2:AB" & lr).SpecialCells(12).Copy Sheet2.Range("A3")
        .AutoFilterMode = False
    End With
    Application.ScreenUpdating = True
    End Sub

  8. #8
    Registered User
    Join Date
    04-27-2017
    Location
    London
    MS-Off Ver
    MS Office 2013
    Posts
    38

    Re: VBA - Extracting unique data based on multiple criteria

    Sintek, you're a legend!

    Are you able to remove duplicates from within that code?

  9. #9
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,292

    Re: VBA - Extracting unique data based on multiple criteria

    Give this version a go...tx jindon
    Sub Extract2()
    Dim Valu, Temp(), i As Long, cnt As Long
    With Sheet1.Cells(1).CurrentRegion.Resize(, 28)
        Valu = .Value: cnt = 0
        For i = 1 To UBound(Valu, 1)
            If Valu(i, 1) = Sheet2.Range("A1") And Valu(i, 11) = Sheet2.Range("B1") Then
                ReDim Preserve Temp(cnt)
                If Not IsNumeric(Application.Match(Valu(i, 28), Temp, 0)) Then
                    Temp(cnt) = Valu(i, 28)
                    cnt = cnt + 1
                End If
            End If
        Next i
    End With
    Sheet2.Range("A3").Resize(cnt) = Application.WorksheetFunction.Transpose(Temp)
    End Sub


    EDit...Forgot...
    Are you able to remove duplicates from within that code?
    Sheet2.Columns("A:A").RemoveDuplicates 1
    Last edited by sintek; 04-18-2019 at 06:39 AM.

  10. #10
    Registered User
    Join Date
    04-27-2017
    Location
    London
    MS-Off Ver
    MS Office 2013
    Posts
    38

    Re: VBA - Extracting unique data based on multiple criteria

    Works brilliantly, thank you very much sintek!

  11. #11
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,292

    Re: VBA - Extracting unique data based on multiple criteria

    ............................................
    Thanks.gif
    Please mark thread as solved...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Distinct function/ Formula or Unique Data based on multiple criteria
    By naveeddil in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-14-2019, 10:49 PM
  2. Extracting rows of data based on multiple criteria from a list
    By cnak in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-04-2018, 12:10 PM
  3. [SOLVED] Extracting unique data based on criteria
    By Jolene78 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-05-2017, 08:19 AM
  4. Extract Unique data based on multiple criteria
    By ajaypal.sp in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-27-2016, 01:13 PM
  5. Adding a date range to a unique data count based on multiple criteria
    By Matthew_Smith86 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-10-2015, 08:50 AM
  6. Extracting Unique Records and Copying into Another Sheet Based on Various Criteria
    By abutler911 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-10-2013, 11:09 AM
  7. Extracting rows based on multiple ranges and unique numbers
    By D13 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-19-2012, 09:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1