VBA Highlight duplicate ID which sum of their value equal zero

Aicha

New Member
Joined
Jan 19, 2019
Messages
7
Dear Community
I need a formula that highlight values which total is zero but have the same reference ID
I want to have the below results.

IDValueResult
A1highlighted on color green
A2
A-1highlighted on color green
C4
C-2highlighted on color green
C2highlighted on color green
A3

<colgroup><col span="2"><col></colgroup><tbody>
</tbody>

I will really appreciate if you can support
Thank you in advance.
 
I don't quite understand the critria.
Let's say the data is below, is the result true?


Book1
ABC
1IDValueResult
2A1
3A1
4A-1
5C4
6C-2
7C-2
Sheet1


So id A won't be highlighted because the total isn't zero, but id C will be highlighted because the total is zero?
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Dear

Yes, exactly the result is true. I don't want want to use ABS because I could have different data for example A=3, A=-2, A=-1, these 3 items should be highlighted since total is zero and have the same reference ID. I think I will need a VBA code to solve this query.
 
Upvote 0
Ok, try this:
Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1084377a()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1084377-vba-highlight-duplicate-id-sum-their-value-equal-zero.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] va, x
[COLOR=Royalblue]Dim[/COLOR] d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]

[COLOR=Royalblue]Set[/COLOR] d = CreateObject([COLOR=brown]"scripting.dictionary"[/COLOR])
d.CompareMode = vbTextCompare
va = Range([COLOR=brown]"A1:B"[/COLOR] & Cells(Rows.count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp).Row)

[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]2[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])
    x = Trim(va(i, [COLOR=crimson]1[/COLOR]))
        [COLOR=Royalblue]If[/COLOR] [COLOR=Royalblue]Not[/COLOR] d.Exists(x) [COLOR=Royalblue]Then[/COLOR]
            d(x) = va(i, [COLOR=crimson]2[/COLOR])
        [COLOR=Royalblue]Else[/COLOR]
            d(x) = d(x) + va(i, [COLOR=crimson]2[/COLOR])
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[COLOR=Royalblue]Next[/COLOR]


Application.ScreenUpdating = [COLOR=Royalblue]False[/COLOR]
[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]2[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])
    [I][COLOR=seagreen]'highlight in col C[/COLOR][/I]
    [COLOR=Royalblue]If[/COLOR] d(Trim(va(i, [COLOR=crimson]1[/COLOR]))) = [COLOR=crimson]0[/COLOR] [COLOR=Royalblue]Then[/COLOR] Cells(i, [COLOR=brown]"C"[/COLOR]).Interior.Color = vbYellow
[COLOR=Royalblue]Next[/COLOR]
Application.ScreenUpdating = [COLOR=Royalblue]True[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
Hello,

You are genius, it's working but I don't understand why some items with the same reference ID wasn't highlighted while their total equal zero. I don't know why!!
 
Upvote 0
Hello,

You are genius, it's working but I don't understand why some items with the same reference ID wasn't highlighted while their total equal zero. I don't know why!!

Not sure why that happened.
Can you upload some sample where it went wrong?
Note: it's easier to check if you sort the data by ID first.
The code works for this made up sample:


Book1
ABC
1IDVALUERESULT
2J-1
3J-1
4K-1
5K1
6M1
7P2
8P-3
9P1
10Q1
11Q2
12R-2
13R2
Sheet1
 
Last edited:
Upvote 0
Hm, your data in col B is Double type. Sometimes it is tricky to deal with, regarding the rounding issue.
So some of your data has total sum of 0 in the worksheet but when calculate it in vba it's not really 0, it's maybe 0,0000000001.
I don't how precise you want the calculation to be, but I revised the macro so if the sum is between -0.000001 and 0.000001 then it is considered 0.
Try this:

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1084377c()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1084377-vba-highlight-duplicate-id-sum-their-value-equal-zero.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] va, x
[COLOR=Royalblue]Dim[/COLOR] d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]

[COLOR=Royalblue]Set[/COLOR] d = CreateObject([COLOR=brown]"scripting.dictionary"[/COLOR])
d.CompareMode = vbTextCompare
va = Range([COLOR=brown]"A1:B"[/COLOR] & Cells(Rows.count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp).Row)
Columns([COLOR=brown]"C"[/COLOR]).Interior.Color = xlNone
[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]2[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])
    x = Trim(va(i, [COLOR=crimson]1[/COLOR]))
        [COLOR=Royalblue]If[/COLOR] [COLOR=Royalblue]Not[/COLOR] d.Exists(x) [COLOR=Royalblue]Then[/COLOR]
            d(x) = va(i, [COLOR=crimson]2[/COLOR])
        [COLOR=Royalblue]Else[/COLOR]
            d(x) = d(x) + va(i, [COLOR=crimson]2[/COLOR])
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[COLOR=Royalblue]Next[/COLOR]

Application.ScreenUpdating = [COLOR=Royalblue]False[/COLOR]
[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]2[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])
    [I][COLOR=seagreen]'highlight in col C[/COLOR][/I]
    [COLOR=Royalblue]If[/COLOR] d(Trim(va(i, [COLOR=crimson]1[/COLOR]))) > -[COLOR=crimson]0.000001[/COLOR] [COLOR=Royalblue]And[/COLOR] d(Trim(va(i, [COLOR=crimson]1[/COLOR]))) < [COLOR=crimson]0.000001[/COLOR] [COLOR=Royalblue]Then[/COLOR] Cells(i, [COLOR=brown]"C"[/COLOR]).Interior.Color = vbYellow
[COLOR=Royalblue]Next[/COLOR]
Application.ScreenUpdating = [COLOR=Royalblue]True[/COLOR]

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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