+ Reply to Thread
Results 1 to 10 of 10

Making Rule work on Select Columns

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2013
    Location
    israel
    MS-Off Ver
    Excel 2010
    Posts
    6

    Making Rule work on Select Columns

    Hey I have this rule which highlights duplicates in a row in different colors so if you have a row that is

    1
    1
    1
    2
    2
    3
    1
    3

    All the one's will be blue 2's red and 1's white. Currently the rule below only works on column A. All my columns have headers, and I need it to instead of working on column A work on the columns with header names I choose.
    Anyone have any idea how this can be accomplished??

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range, Dn As Range
    Dim K
    Dim col
    Dim c As Integer
    If Target.Column = 1 Then
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
    For Each Dn In Rng
    If Not .Exists(Dn.Value) Then
    .Add Dn.Value, Dn
    Else
    Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
    End If
    Next
    col = Array(3, 50, 6, 7, 8, 34, 35, 38, 39, 50, 45, 46)
    For Each K In .keys
    If .Item(K).Count > 1 Then
    c = IIf(c = 12, 0, c)
    .Item(K).Interior.ColorIndex = col(c)
    c = c + 1
    End If
    Next K
    End With
    End If
    End Sub

  2. #2
    Registered User
    Join Date
    03-10-2013
    Location
    israel
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Making Rule work on Select Columns

    Anyone have an idea?

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Making Rule work on Select Columns

    shba,

    Welcome to the forum! In the future, please wrap your code in code tags (see link in my sig for how).
    As for your question, you need to change the If Target.Column = 1 line. So if you wanted the code to monitor columns A, C and K (which are column numbers 1, 3, and 11) the line would look like this:
    If InStr(1, " " & Target.Column & " ", " 1 3 11 ", vbTextCompare) > 0 Then
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Making Rule work on Select Columns

    shba,

    I received your PM which contained the following:
    Quote Originally Posted by shba
    Quote Originally Posted by tigeravatar View Post
    shba,

    Welcome to the forum! In the future, please wrap your code in code tags (see link in my sig for how).
    As for your question, you need to change the If Target.Column = 1 line. So if you wanted the code to monitor columns A, C and K (which are column numbers 1, 3, and 11) the line would look like this:
    If InStr(1, " " & Target.Column & " ", " 1 3 11 ", vbTextCompare) > 0 Then

    Hey thank you for the reply, but this isn't my question. I need this rule to work on many different files A, C and K won't always be the correct columns I need the rule to work on the column with the name IP but that column will sometimes be column A other times C and so forth.

    Thanks
    Yair

    Ugly part first, from the forum rules:
    4. Don't Private Message or email Excel questions to moderators or other members. (or Word, Access, etc.) The point of having a public forum is to share solutions to common (and sometimes uncommon) problems with all members.


    Alright, on to your question. So basically the Target.Column needs to be dynamic. You can accomplish this using the Range.Find method like so:
        Dim rngFound As Range
        
        Set rngFound = Target.Parent.Rows(1).Find("IP", , xlValues, xlWhole)
        If rngFound Is Nothing Then Exit Sub
        If Target.Column = rngFound.Column Then

  5. #5
    Registered User
    Join Date
    03-10-2013
    Location
    israel
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Making Rule work on Select Columns

        Dim rngFound As Range
        
        Set rngFound = Target.Parent.Rows(1).Find("IP", , xlValues, xlWhole)
        If rngFound Is Nothing Then Exit Sub
        If Target.Column = rngFound.Column Then
    Thank you, I think this is exactly what I needed, I'm having trouble combining them, adding the code instead of
    If Target.Column = 1 Then
    I also tried replacing it with
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    but I get an error in this part of the code
    For Each Dn In Rng

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Making Rule work on Select Columns

    shba,

    Thank you for using code tags
    I think the full code would look like this:
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim Rng As Range, Dn As Range, rngFound As Range
        Dim K
        Dim col
        Dim c As Integer
        
        Set rngFound = Target.Parent.Rows(1).Find("IP", , xlValues, xlWhole)
        If rngFound Is Nothing Then Exit Sub
        If Target.Column = rngFound.Column Then
            Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
            With CreateObject("scripting.dictionary")
                .CompareMode = vbTextCompare
                For Each Dn In Rng
                    If Not .Exists(Dn.Value) Then
                        .Add Dn.Value, Dn
                    Else
                        Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
                    End If
                Next
                col = Array(3, 50, 6, 7, 8, 34, 35, 38, 39, 50, 45, 46)
                For Each K In .keys
                    If .Item(K).Count > 1 Then
                        c = IIf(c = 12, 0, c)
                        .Item(K).Interior.ColorIndex = col(c)
                        c = c + 1
                    End If
                Next K
            End With
        End If
        
    End Sub

  7. #7
    Registered User
    Join Date
    03-10-2013
    Location
    israel
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Making Rule work on Select Columns

    Thanks, It's kind of working, now it will work on column A and if the column header is IP. I've tried changing the Range, but I keep getting errors no matter how I change it , I need it to work on the entire document.

    Also If i want to add another name which will work just add something after the IP? so for example
    Set rngFound = Target.Parent.Rows(1).Find("IP","ID","NAME" , xlValues, xlWhole)

  8. #8
    Registered User
    Join Date
    03-10-2013
    Location
    israel
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Making Rule work on Select Columns

    Anyone have an idea?

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Making Rule work on Select Columns

    shba,

    Updated code, should address both issues:
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim Rng As Range, Dn As Range, rngFound As Range
        Dim K
        Dim col
        Dim c As Integer
        Dim varFind As Variant
        
        For Each varFind In Array("IP", "ID", "NAME")
            Set rngFound = Target.Parent.Rows(1).Find(varFind, , xlValues, xlWhole)
            If Not rngFound Is Nothing Then
                If Target.Column = rngFound.Column Then
                    Set Rng = Range(Cells(1, Target.Column), Cells(Rows.Count, Target.Column).End(xlUp))
                    With CreateObject("scripting.dictionary")
                        .CompareMode = vbTextCompare
                        For Each Dn In Rng
                            If Not .Exists(Dn.Value) Then
                                .Add Dn.Value, Dn
                            Else
                                Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
                            End If
                        Next
                        col = Array(3, 50, 6, 7, 8, 34, 35, 38, 39, 50, 45, 46)
                        For Each K In .keys
                            If .Item(K).Count > 1 Then
                                c = IIf(c = 12, 0, c)
                                .Item(K).Interior.ColorIndex = col(c)
                                c = c + 1
                            End If
                        Next K
                    End With
                End If
            End If
        Next varFind
        
    End Sub

  10. #10
    Registered User
    Join Date
    03-10-2013
    Location
    israel
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Making Rule work on Select Columns

    Works great thank you very much for all your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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