Macro or Formula to match positive and negative numbers

Madzark

New Member
Joined
May 8, 2016
Messages
17
Dear community,

I need to match up a negative number with a positive number that would make the value zero. I tried to use the COUNTIF formula (e.g. =COUNTIF($Q$2:$Q$30210;Q2)<>COUNTIF($Q$2:$Q$30210;-Q2)) but it doesn't work at all.

Any suggestions? I would like to run a macro, because i have files with over 30.000 rows and filters are running late.

Thank you very much!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
What should happen when you find positive and negative numbers that match?
Could you post an example?
 
Last edited:
Upvote 0
The macro should eliminate the rows it has identified as perfect pairs of positives and negatives or , at least, assign a value that allows me to filter the results
 
Upvote 0
Try this:
Data at column Q, the result in col R, the ones with "xx" is positive and negative numbers that match.

Code:
[FONT=lucida console][color=Royalblue]Sub[/color] positive_negative_match1c()
[i][color=seagreen]'use this[/color][/i]
[i][color=seagreen]'positive-negative match pair[/color][/i]
[i][color=seagreen]'without grouping[/color][/i]

[color=Royalblue]Dim[/color] i [color=Royalblue]As[/color] [color=Royalblue]Long[/color]
[color=Royalblue]Dim[/color] va, z, q, ary1, ary2
[color=Royalblue]Dim[/color] d [color=Royalblue]As[/color] [color=Royalblue]Object[/color]
 

[i][color=seagreen]'data start at Q2[/color][/i]
va = Range([color=brown]"Q2"[/color], Cells(Rows.count, [color=brown]"Q"[/color]).[color=Royalblue]End[/color](xlUp))

[color=Royalblue]Set[/color] d = CreateObject([color=brown]"scripting.dictionary"[/color])

[color=Royalblue]For[/color] i = [color=crimson]1[/color] [color=Royalblue]To[/color] UBound(va, [color=crimson]1[/color])
    z = va(i, [color=crimson]1[/color]):  d(z) = d(z) & [color=brown]","[/color] & i
[color=Royalblue]Next[/color]


[color=Royalblue]For[/color] [color=Royalblue]Each[/color] q [color=Royalblue]In[/color] d.Keys
    [color=Royalblue]If[/color] q > [color=crimson]0[/color] [color=Royalblue]And[/color] d.Exists(-q) [color=Royalblue]Then[/color]
        ary1 = Split(d(q), [color=brown]","[/color]):  ary2 = Split(d(-q), [color=brown]","[/color])
    
        [color=Royalblue]If[/color] UBound(ary1) < UBound(ary2) [color=Royalblue]Then[/color] P = UBound(ary1) [color=Royalblue]Else[/color] P = UBound(ary2)
            [color=Royalblue]For[/color] i = [color=crimson]1[/color] [color=Royalblue]To[/color] P
                va(ary1(i), [color=crimson]1[/color]) = [color=brown]"xx"[/color]: va(ary2(i), [color=crimson]1[/color]) = [color=brown]"xx"[/color]
            [color=Royalblue]Next[/color]
    [color=Royalblue]End[/color] [color=Royalblue]If[/color]
[color=Royalblue]Next[/color]

[i][color=seagreen]'put the result in col R[/color][/i]
Range([color=brown]"R2"[/color]).Resize(UBound(va, [color=crimson]1[/color]), [color=crimson]1[/color]) = va

[color=Royalblue]End[/color] [color=Royalblue]Sub[/color][/FONT]




Excel 2013
QR
1No
22xx
33xx
44xx
555
666
77xx
888
999
101010
111xx
1222
1333
1444
1555
1666
1777
1888
1999
201010
21-1xx
22-2xx
23-3xx
24-4xx
25-7xx
Sheet2


Edited:
wrong code:
va = Range("Q2", Cells(Rows.count, "A").End(xlUp))
 
Last edited:
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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