Complex Formula Value Distribute

aqeelnokia99

Board Regular
Joined
Dec 23, 2018
Messages
145
Office Version
  1. 2013
Platform
  1. Windows
Hi Experts i try to Distribute Value from Table 1 Until its Reached Value 1000000 if its Reached Write Remaining amount and start from again from Remain amount for Example
if you see Result from Value 1130 to 465001 Total is 979026 to Reach @ 1000000 we need more 1000000-979026 =20974 This amount will come from Table 1 SR # 5 Which is 250000 After That is you see Result Table next value is 229026 which = to 250000-20974 so on in Simple Distribute Value of table 1 Value Until its Reached 1000000 if its Possible with formula or Vba will Very Big Thanks

Table 11000000
SR#EurosResult
111301130
2171361171361
3341534341534
4465001465001
525000020974This Part is Remaing Balance to Reach amount 1000000
6390854229026Start from again from 250000-20974
7307572390854
8710127307572
960000072548
10260208637579
11500000362421
12274438
 
Hi @aqeelnokia99. Thanks for posting on the forum.

if its Possible with formula or Vba
I show you a possible solution with VBA. (Help columns are not required)
Important. The values are read from cells C3 and up to the last row with values from column C, the result in cell G3 onwards. If you modify the source or destination cells, we need to adjust the macro.

VBA Code:
Sub Distribute_Value()
  Dim i As Long, k As Long
  Dim nTop As Double, nSum As Double
  Dim a As Variant, b As Variant
 
  nTop = 1000000
  a = Range("C3", Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1) * 3, 1 To 1)
 
  For i = 1 To UBound(a)
    nSum = nSum + a(i, 1)
    k = k + 1
    If nSum < nTop Then
      b(k, 1) = a(i, 1)
    ElseIf nSum = nTop Then
      b(k, 1) = a(i, 1)
      nSum = 0
    Else
      b(k, 1) = nTop - (nSum - a(i, 1))
      b(k + 1, 1) = a(i, 1) - b(k, 1)
      nSum = b(k + 1, 1)
      k = k + 1
    End If
  Next
  Range("G3").Resize(UBound(b, 1)).Value = b
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Last edited:
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hopefully now you should not have any problem :

Book2
ABCDEFGHIJKLM
101/04/20231,000,000
2SR#DateEurosExchange RateBalance EurosResultHelper1Helper2Helper3Helper4
3108/11/2022200,000223.94 200,000Apr-23200,000200,000 200,000
4231/01/2023171,361288.29 171,361Apr-23371,361171,361
5331/01/2023341,534288.29 341,534Apr-23712,895341,534 171,361
6409/02/2023465,001290.11€ 177,896287,105Apr-231,177,896287,105177,896
7513/02/2023250,000279.98 177,896May-23427,896250,000 341,534
8613/02/2023390,854288.36 250,000May-23818,750390,854
9721/02/2023307,572279.43€ 126,322390,854May-231,126,322181,250126,322287,105
10827/02/2023710,127274.01 181,250May-23836,449710,127 177,896
11909/03/2023600,000297.7€ 436,449126,322May-231,436,449163,551436,449250,000
121013/03/2023260,208302.2 710,127Jun-23696,657260,208
131121/03/2023500,000302€ 196,657163,551Jun-231,196,657303,343196,657390,854
141228/03/2023274,438304.81 436,449Jul-23471,095274,438
15260,208Jul-23471,095- 181,250
16303,343Jul-23471,095- 126,322
17196,657Aug-23471,095- 710,127
18274,438Aug-23471,095-
19-Aug-23471,095- 163,551
20-Aug-23471,095- 436,449
21-Aug-23471,095- 260,208
22 471,095-
23 471,095- 303,343
24 471,095- 196,657
25471,095- 274,438
26471,095-
27471,095- -
28471,095-
29471,095- -
30471,095-
31471,095- -
Sheet1
Cell Formulas
RangeFormula
J3J3=IF(J2>$G$1,C3,J2+C3)
K3K3=IF(J3>$G$1,IF($G$1*COUNTIF($J$3:J3,">"&$G$1)-SUM($K2:L$3)=0,C3,$G$1*COUNTIF($J$3:J3,">"&$G$1)-SUM($K2:L$3)),C3)
L3L3=IF($G$1*COUNTIF($J$3:J3,">"&$G$1)-SUM($K$3:K3)=0,C3-K3,"")
M3:M31M3=IFERROR(INDEX($K$3:$L$31,1+INT((ROW(A1)-1)/2),MOD(ROW(A1)-1+2,2)+1),"")
J4:J31J4=IF(J3>$G$1,C4+L3,J3+C4)
K4:K31K4=IF(J4>$G$1,IF($G$1*COUNTIF($J$3:J4,">"&$G$1)-SUM($K$3:L3)=0,C4,$G$1*COUNTIF($J$3:J4,">"&$G$1)-SUM($K$3:L3)),C4)
L4:L31L4=IF($G$1*COUNTIF($J$3:J4,">"&$G$1)-SUM($K$3:K4)-SUM($L$3:L3)=0,C4-K4,"")
E3:E14E3=L3
G3:G24G3=IFERROR(INDEX(M:M,SMALL(IF(M$3:M$1000<>"",ROW(M$3:M$1000)), ROWS(M$3:M3))), "")
This look awesome i
 
Upvote 0
Hi @aqeelnokia99. Thanks for posting on the forum.


I show you a possible solution with VBA. (Help columns are not required)
Important. The values are read from cells C3 and up to the last row with values from column C, the result in cell G3 onwards. If you modify the source or destination cells, we need to adjust the macro.

VBA Code:
Sub Distribute_Value()
  Dim i As Long, k As Long
  Dim nTop As Double, nSum As Double
  Dim a As Variant, b As Variant
 
  nTop = 1000000
  a = Range("C3", Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1) * 3, 1 To 1)
 
  For i = 1 To UBound(a)
    nSum = nSum + a(i, 1)
    k = k + 1
    If nSum < nTop Then
      b(k, 1) = a(i, 1)
    ElseIf nSum = nTop Then
      b(k, 1) = a(i, 1)
      nSum = 0
    Else
      b(k, 1) = nTop - (nSum - a(i, 1))
      b(k + 1, 1) = a(i, 1) - b(k, 1)
      nSum = b(k + 1, 1)
      k = k + 1
    End If
  Next
  Range("G3").Resize(UBound(b, 1)).Value = b
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
Awesome Big Thanks
 
Upvote 1

Forum statistics

Threads
1,215,510
Messages
6,125,223
Members
449,216
Latest member
biglake87

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