Consulting

Results 1 to 17 of 17

Thread: Macro to sort Ascending,identify duplicates& highlight yellow

  1. #1
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location

    Smile Macro to sort Ascending,identify duplicates& highlight yellow

    HI,

    Trying to build a macro- to sort the list(ascending) of numbers in column A and identify duplicates and highlight them yellow. Only catch to this is one month the list might have 100 cells information, next month it only be 50.

    I am guessing it will incorporate if then else statement(for identifying duplicates) and relative range to do both sorting.

    Any suggestions is greatly appreciated.

  2. #2
    VBAX Regular
    Joined
    Jan 2009
    Posts
    93
    Location

    You can try to use formula in conditional formating

    Hi, You can try this, firstly mark column A = select, then set Format, Conditional formating, use formula, set custom format.

    formula:

    =COUNTIF(A:A,A1)>1

    =COUNTIF(A:A;A1)>1 country oriented, works in Czech Republic

    http://www.mrexcel.com/articles/dupl...formatting.php

    GOOD LUCK AND GOOD NIGHT

    Pavel Humenuk
    Last edited by hardlife; 03-17-2009 at 09:42 AM.

  3. #3
    VBAX Regular
    Joined
    Jan 2009
    Posts
    93
    Location

    Smile Macro

    Sub SortAndMarkDuplicatesInColumnA()

    Columns("A:A").Select
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    Columns("A:A").Select
    'Selection.FormatConditions.Delete
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=COUNTIF(A:A,A1)>1" 'for our country Czech Republic use "=COUNTIF(A:A;A1)>1"
    Selection.FormatConditions(1).Interior.ColorIndex = 27

    End Sub

    GOOD LUCK AND GOOD NIGHT

    Pavel Humenuk
    Last edited by hardlife; 03-17-2009 at 09:43 AM.

  4. #4
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location

    Thanks!!

    Thanks for helping-

    1) Conditional formatting is not doing anything-Followed step by step procedure.

    2) Tried Macro - IT SAYS - Compile Error:
    Expected:Expression

    Sorry little new @ this.

    Thanks Again.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try this version of Hardlife's code
    [VBA]
    Option Explicit
    Sub SortAndMarkDuplicatesInColumnA()
    Range("A1").Select
    With Columns("A:A")
    .Sort Key1:=Range("A2"), Order1:=xlAscending
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF(A:A,A1)>1"
    .FormatConditions(1).Interior.ColorIndex = 27
    End With
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Pavel,
    Problem I think is separator in "=COUNTIF(A:A;A1)>1". Comma instead of semi-colon.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location

    You guys r Rockstars!! Thanks

    Thanks Guys it really worked!! here is another question what if i want to extended the sorting to column J as Column A Numbers have information extended till column J.

    I am trying to learn about Range- eventually wanna sort column A to J and highlight the duplicates.

    Thanks a lot..

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Option Explicit
    Sub SortAndMarkDuplicatesInColumnA()
    Range("A1").Select
    With Columns("A:J")
    .Sort Key1:=Range("A2"), Order1:=xlAscending
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($A:$A,$A1)>1"
    .FormatConditions(1).Interior.ColorIndex = 27
    End With
    End Sub
    [/vba]
    ____________________________________________
    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

  9. #9
    VBAX Regular
    Joined
    Jan 2009
    Posts
    93
    Location

    Me is also learning, Thanks to Everybody

    It is beautiful to see clear code, Thanks a lot to Everybody,
    It is also beautiful to see help pro users to us,

    mdmackillop, xld and Doodle80, all other users,
    wish You to have a nice day,
    Pavel Humenuk

  10. #10
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location
    Thanks everyone!!

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Bob,
    Out of interest, can we set the Conditional Formatting without selecting A1 first? My attempts were unsuccessful.
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Regular
    Joined
    Jan 2009
    Posts
    93
    Location

    It could be possible only if it is range, not column

    =COUNTIF($A$2:$A$10,A2)>1

    happy and sunny winter day to everybody

    Pavel Humenuk

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    That gives me
    =COUNTIF($A$2:$A$10,IT65535)>1
    or using $A2 I get
    =COUNTIF($A$2:$A$10,$A65535)>1
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes you can, but you have to adjust it according to what is selected.

    For instance, suppose you want a formula of =A1>7 in the range A1:E10, this will work wherever you are

    [vba]

    'just to prove it works
    Range("M17").Select
    With Range("A1:E10")

    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, _
    Formula1:="=" & ActiveCell.Address(False, False) & ">7"
    .FormatConditions(1).Interior.ColorIndex = 3
    End With
    [/vba]
    ____________________________________________
    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

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    In the example in this thread, it would be

    [vba]

    Sub SortAndMarkDuplicatesInColumnA()
    Dim mpFormula As String

    With Columns("A:J")
    .Sort Key1:=Range("A2"), Order1:=xlAscending
    .FormatConditions.Delete
    mpFormula = "=COUNTIF($A:$A,$" & ActiveCell.Address(False, False) & ")>1"
    .FormatConditions.Add Type:=xlExpression, Formula1:=mpFormula
    .FormatConditions(1).Interior.ColorIndex = 27
    End With
    End Sub
    [/vba]
    ____________________________________________
    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

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Bob, I'll have a play with that method.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    VBAX Regular
    Joined
    Jan 2009
    Posts
    93
    Location

    Smile This is office behavior, must to edit it twice

    May be me is wrong, but it happened to me in office2007,
    if me pasted formula, conditional formating was set wrongly,
    so me edited it again and it was OK, if it was the question.

    when it is not working select A2:A10 and
    edit stored conditional formating again,
    Pavel

    =COUNTIF($A$2:$A$10,A2)>1

    Quote Originally Posted by mdmackillop
    That gives me
    =COUNTIF($A$2:$A$10,IT65535)>1
    or using $A2 I get
    =COUNTIF($A$2:$A$10,$A65535)>1
    Last edited by hardlife; 03-17-2009 at 01:16 PM.

Posting Permissions

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