Consulting

Results 1 to 9 of 9

Thread: Sorting table with Duplicates

  1. #1
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    5
    Location

    Sorting table with Duplicates

    I'm trying to learn VBA and I don't where to start with this. I think this shouldn't be difficult, but I'm just going around in circles with my attempts.
    I'm trying to write a macro to sort data in a table. I need to delete the duplicates in only column A without affecting the location of the remaining text.
    What I have:
    Employee Certification Series CATEGORY
    Joe 1/1/19 DS DEMO
    Joe 8/7/18 EE CLASS
    Joe 3/14/19 EE OBSERVATION
    Claire 1/5/19 K7 CLASS
    Claire 2/17/19 DS DEMO
    Mary 6/9/18 RG WRITTEN
    William 11/28/18 EM WRITTEN
    What I'm trying to do:
    Employee Certification Series CATEGORY
    1/1/19 DS DEMO
    8/7/18 EE CLASS
    3/14/19 EE OBSERVATION
    Claire 1/5/19 K7 CLASS
    2/17/19 DS DEMO
    Mary 6/9/18 RG WRITTEN
    William 11/28/18 EM WRITTEN
    I greatly appreciate your help!

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    It's the For iName loop that does the work

    Option Explicit
    Sub SortAndFormat()
        Dim wsData As Worksheet
        Dim rData As Range, rDataWithoutHeaders As Range
        Dim iName As Long
        
        Application.ScreenUpdating = False
        
        Set wsData = Worksheets("Sheet1")
        
        Set rData = wsData.Cells(1, 1).CurrentRegion
        Set rDataWithoutHeaders = rData.Cells(2, 1).Resize(rData.Rows.Count - 1, rData.Columns.Count)
        
    
        With wsData.Sort
            .SortFields.Clear
            .SortFields.Add Key:=rDataWithoutHeaders.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SortFields.Add Key:=rDataWithoutHeaders.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .SetRange rData
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    
        For iName = rData.Rows.Count To 2 Step -1
            If rData.Cells(iName, 1).Value = rData.Cells(iName - 1, 1) Then rData.Cells(iName, 1).ClearContents
        Next
    
    
        Application.ScreenUpdating = True
    End Sub
    
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not just use conditional formatting with a formula of
    =COUNTIF($A:$A,$A2)>1
    and set the font to white.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Quote Originally Posted by Paul_Hossler View Post
       Set rDataWithoutHeaders = rData.Cells(2, 1).Resize(rData.Rows.Count - 1, rData.Columns.Count)
    
    If in the defined range we do not change the number of columns, then this parameter does not have to be given (and similarly for the rows):
    Set rDataWithoutHeaders = rData.Offset(1).Resize(rData.Rows.Count - 1)
    'or in the case of rows
    Set rDataWithoutHeadersAndLastCol = rData.Offset(1).Resize(, rData.Columns.Count - 1)
    Quote Originally Posted by xld View Post
    Why not just use CF (...) and set the font to white.
    Some printers can not handle this task. Although we see "nothing" on the screen, they will show the contents of the cell on a sheet of paper.

    Artik

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    If in the defined range we do not change the number of columns, then this parameter does not have to be given (and similarly for the rows):
    I just consider it more robust to do it this way

    I always like to spell things out
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    5
    Location
    Hello Paul, Thank you so much. I ran each step and everything was good until Application.ScreenUpdating = True. That deleted blanks in between the names in column A and then the data for each person was off. WHy would I need that line of code? Thank you so much, again!

  7. #7
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    5
    Location
    Hello xld,

    I had considered that, but I also want to merge the cells for a single person's name and center the text.
    Thank you for your reply. I have taken your formula to experiment using it with other data.
    Thank you again!

  8. #8
    VBAX Newbie
    Joined
    Jun 2019
    Posts
    5
    Location
    Hello Paul,
    I appreciate your giving me all of the details. It really helps us newbies!

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by RebaS View Post
    Hello Paul, Thank you so much. I ran each step and everything was good until Application.ScreenUpdating = True. That deleted blanks in between the names in column A and then the data for each person was off. WHy would I need that line of code? Thank you so much, again!

    That line should not do things like that

    It didn't in my file in #2

    Is your data all in one 'block' or do you have seperator columns?

    Before.JPG After.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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