Need help with Range in VBA code.

shba

New Member
Joined
Mar 10, 2013
Messages
14
Hi the code below is a rule which highlights duplicates in different colors, can anyone help me with the Set Rng bit, I need it to simply work on the entire document not just column A.

Also the code in italics cannot be changed I need it to work on the entire document AS LONG AS it is on columns I choose.
Code:
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
Thank you
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi

Do you need the Rng to be the same column as the changed value (ie Target)? If so then maybe:

Code:
'Replace this:  Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))

'With:

Set Rng = Application.Intersect(Target.EntireColumn,Me.UsedRange)
 
Upvote 0
Hi

Do you need the Rng to be the same column as the changed value (ie Target)? If so then maybe:

Code:
'Replace this:  Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))

'With:

Set Rng = Application.Intersect(Target.EntireColumn,Me.UsedRange)


Doesn't work, here is a test file
https://www.dropbox.com/s/fov76o2lm56y8h3/test.xlsx

I need to find a way for the code to work on each column and not just the first and to be able to name which columns it works so if looking at the example to tell it just work on IP and Numbers and not on Payout Method.


Thanks
 
Upvote 0

Forum statistics

Threads
1,207,421
Messages
6,078,436
Members
446,337
Latest member
nrijkers

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