Macro sum between all positive and negative entries to reach a value

hstef

New Member
Joined
Nov 19, 2018
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have a column (L) with some negative and positive entries (invoices value).
I have some customers who pay thru bank and does not specify which invoices is paying. And usually do not pay in order.
All i have is paymant's total value.

I need a macro to make calculation in column "L" until it reaches the total value of payment and to highlight the cells that made up the value.
I will put the payment value in "O1".

Column L example:
203.49
224.91
-224.91
239.67
-36.48
-203.49
64.26
-66.96
24.99
-24.99
105.43

<colgroup><col></colgroup><tbody>
</tbody>

Can this be done?
Thank you kindly,
Stef.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hello,

Quite a common question ...

Are you trying to create an account reconciliation ...?
 
Upvote 0
Hi,

Not really. Just to find out what invoices are paid with a bank payment.

Thank you for your reply.
 
Upvote 0
Try this:-
Values summing to zero, coloured yellow.
Sum of remaining values in "O1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG19Nov55
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dic         [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] Q           [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] n1          [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n2          [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
   
   [COLOR="Navy"]Set[/COLOR] Rng = Range(Range("L2"), Range("L" & Rows.Count).End(xlUp))
     [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
        Dic.CompareMode = 1
   [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
         n1 = 0: n2 = 0
         [COLOR="Navy"]If[/COLOR] Not Dic.Exists(Abs(Dn.Value)) [COLOR="Navy"]Then[/COLOR]
                ReDim ray(1 To Rng.Count, 1 To 2)
                [COLOR="Navy"]If[/COLOR] Dn.Value > 0 [COLOR="Navy"]Then[/COLOR]
                    [COLOR="Navy"]Set[/COLOR] ray(1, 1) = Dn
                    n1 = n1 + 1
                [COLOR="Navy"]Else[/COLOR]
                    [COLOR="Navy"]Set[/COLOR] ray(1, 2) = Dn
                    n2 = n2 + 1
                [COLOR="Navy"]End[/COLOR] If
                Dic.Add (Abs(Dn.Value)), Array(ray, n1, n2)
        [COLOR="Navy"]Else[/COLOR]
                Q = Dic(Abs(Dn.Value))
                    [COLOR="Navy"]If[/COLOR] Dn.Value > 0 [COLOR="Navy"]Then[/COLOR]
                        Q(1) = Q(1) + 1
                        [COLOR="Navy"]Set[/COLOR] Q(0)(Q(1), 1) = Dn
                    [COLOR="Navy"]Else[/COLOR]
                        Q(2) = Q(2) + 1
                        [COLOR="Navy"]Set[/COLOR] Q(0)(Q(2), 2) = Dn
                    [COLOR="Navy"]End[/COLOR] If
                Dic(Abs(Dn.Value)) = Q
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Dn
 
   
   [COLOR="Navy"]Dim[/COLOR] k [COLOR="Navy"]As[/COLOR] Variant
   [COLOR="Navy"]Dim[/COLOR] oSum [COLOR="Navy"]As[/COLOR] Double
   [COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
   
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] Dic.Keys
            [COLOR="Navy"]For[/COLOR] n = 1 To UBound(Dic(k)(0), 1)
                [COLOR="Navy"]If[/COLOR] Dic(k)(0)(n, 1) <> "" [COLOR="Navy"]Then[/COLOR]
                   [COLOR="Navy"]If[/COLOR] Dic(k)(0)(n, 1) + Dic(k)(0)(n, 2) = 0 [COLOR="Navy"]Then[/COLOR]
                        Dic(k)(0)(n, 1).Interior.Color = vbYellow
                        Dic(k)(0)(n, 2).Interior.Color = vbYellow
                   [COLOR="Navy"]End[/COLOR] If
               [COLOR="Navy"]End[/COLOR] If
           [COLOR="Navy"]Next[/COLOR] n
      
  [COLOR="Navy"]Next[/COLOR] k
   [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] Not Dn.Interior.Color = vbYellow [COLOR="Navy"]Then[/COLOR]
            oSum = oSum + Dn.Value
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
 Range("O1").Value = oSum

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi, sorry James, but this solution is not working. It freezes excel and no matter how long i wait it does not do anything. But thank you kindly for your suggestion.
 
Upvote 0
Hi MickG. Unfortunately your solution gave me different results. The highlighted cells does not represent the sum of the payment. But thank you kindly for your solution.
 
Upvote 0
If you could show a example of your data that fails, with the correct expected result in "O1". We could have another look.
 
Upvote 0
Hi,

I have a column (L) with some negative and positive entries (invoices value).
I have some customers who pay thru bank and does not specify which invoices is paying. And usually do not pay in order.
All i have is paymant's total value.

I need a macro to make calculation in column "L" until it reaches the total value of payment and to highlight the cells that made up the value.
I will put the payment value in "O1".

Using your example above, what should be the results?
 
Upvote 0
I'm not sure I follow the brief here. Looking at your sample I can see one-to-one matches but I cannot see how debits are met by a series of credits, or credits met by a series of debits. So assuming one-to-one match we can produce an analysis to identify all one-to-one matches so that you are left with a list of exceptions to attempt to match instead.

Excel 2010
Row\Col
A
B
C
D
E
F
G
H
1
Vals+ Count- CountInstanceMatchcheck
=SUMIF(E:E,TRUE,A:A)
2
203.49​
=COUNTIF(A:A,ABS(A2))​
=COUNTIF(A:A,-ABS(A2))​
=COUNTIF($A$2:A2,A2)​
=AND(D2<=B2,D2<=C2)​
3
224.91​
=COUNTIF(A:A,ABS(A3))​
=COUNTIF(A:A,-ABS(A3))​
=COUNTIF($A$2:A3,A3)​
=AND(D3<=B3,D3<=C3)​
4
(224.91)
=COUNTIF(A:A,ABS(A4))​
=COUNTIF(A:A,-ABS(A4))​
=COUNTIF($A$2:A4,A4)​
=AND(D4<=B4,D4<=C4)​
5
239.67​
=COUNTIF(A:A,ABS(A5))​
=COUNTIF(A:A,-ABS(A5))​
=COUNTIF($A$2:A5,A5)​
=AND(D5<=B5,D5<=C5)​
6
(36.48)
=COUNTIF(A:A,ABS(A6))​
=COUNTIF(A:A,-ABS(A6))​
=COUNTIF($A$2:A6,A6)​
=AND(D6<=B6,D6<=C6)​
7
(203.49)
=COUNTIF(A:A,ABS(A7))​
=COUNTIF(A:A,-ABS(A7))​
=COUNTIF($A$2:A7,A7)​
=AND(D7<=B7,D7<=C7)​
8
64.26​
=COUNTIF(A:A,ABS(A8))​
=COUNTIF(A:A,-ABS(A8))​
=COUNTIF($A$2:A8,A8)​
=AND(D8<=B8,D8<=C8)​
9
(66.96)
=COUNTIF(A:A,ABS(A9))​
=COUNTIF(A:A,-ABS(A9))​
=COUNTIF($A$2:A9,A9)​
=AND(D9<=B9,D9<=C9)​
10
24.99​
=COUNTIF(A:A,ABS(A10))​
=COUNTIF(A:A,-ABS(A10))​
=COUNTIF($A$2:A10,A10)​
=AND(D10<=B10,D10<=C10)​
11
(24.99)
=COUNTIF(A:A,ABS(A11))​
=COUNTIF(A:A,-ABS(A11))​
=COUNTIF($A$2:A11,A11)​
=AND(D11<=B11,D11<=C11)​
12
105.43​
=COUNTIF(A:A,ABS(A12))​
=COUNTIF(A:A,-ABS(A12))​
=COUNTIF($A$2:A12,A12)​
=AND(D12<=B12,D12<=C12)​
Sheet: Sheet1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,466
Members
449,086
Latest member
kwindels

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