Hi,
I am not sure if I am doing this right but here is the thing:
> I have a list of transaction ID-ed from 1 to 30
> Each of the transaciton is a type P, D, G respectively for Purchased, Debited and Gifted
> P and G are credit transactions
> When the type is D, the oldest credited transaction is the first used (First in First out)
> I need to know at any time, what is the balance as of gifts and as of purchases
To do so, I built an array for purchase and an array for gifts.
I try to redim each of those array everytime but I am not sure whether I am doing it the right way. Problem 1
Problem 2, when a D is encountered because I need to compare the lower bound of the array P and the array G to know which of the balance P or G I am going to substract the amount encountered => I do not manage to take the lower bound of my arrray
Problem 3, once I have "used" the oldest transaction from P or G, I would like to get rid of the line in the array in order to save the memory and ease the research of the lowerbound... but unless I use a temporary array, I am not sure whether this would be the best method...
I hope that somebody will find an interest in helping me going through that.
Thank you very much in advance.
Kind Regards,
Shiro
When I run the code at the bottom of the message on the table below, I populate Bal P, Bal G but I do not manage the part where I redim the arrays
To do so, I have created the table below:
[TABLE="width: 438"]
<tbody>[TR]
[TD]ID
[/TD]
[TD]Amount[/TD]
[TD]Type[/TD]
[TD]Bal P[/TD]
[TD]Bal G[/TD]
[TD]Lbound G[/TD]
[TD]Lbound P[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]10[/TD]
[TD]P[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]10[/TD]
[TD]G[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]60[/TD]
[TD]D[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]40[/TD]
[TD]G[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]10[/TD]
[TD]D[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]6[/TD]
[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TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]
I am not sure if I am doing this right but here is the thing:
> I have a list of transaction ID-ed from 1 to 30
> Each of the transaciton is a type P, D, G respectively for Purchased, Debited and Gifted
> P and G are credit transactions
> When the type is D, the oldest credited transaction is the first used (First in First out)
> I need to know at any time, what is the balance as of gifts and as of purchases
To do so, I built an array for purchase and an array for gifts.
I try to redim each of those array everytime but I am not sure whether I am doing it the right way. Problem 1
Problem 2, when a D is encountered because I need to compare the lower bound of the array P and the array G to know which of the balance P or G I am going to substract the amount encountered => I do not manage to take the lower bound of my arrray
Problem 3, once I have "used" the oldest transaction from P or G, I would like to get rid of the line in the array in order to save the memory and ease the research of the lowerbound... but unless I use a temporary array, I am not sure whether this would be the best method...
I hope that somebody will find an interest in helping me going through that.
Thank you very much in advance.
Kind Regards,
Shiro
When I run the code at the bottom of the message on the table below, I populate Bal P, Bal G but I do not manage the part where I redim the arrays
To do so, I have created the table below:
[TABLE="width: 438"]
<tbody>[TR]
[TD]ID
[/TD]
[TD]Amount[/TD]
[TD]Type[/TD]
[TD]Bal P[/TD]
[TD]Bal G[/TD]
[TD]Lbound G[/TD]
[TD]Lbound P[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]10[/TD]
[TD]P[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]10[/TD]
[TD]G[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]60[/TD]
[TD]D[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]40[/TD]
[TD]G[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]10[/TD]
[TD]D[/TD]
[TD] [/TD]
[TD] [/TD]
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TD]30[/TD]
[TD]100[/TD]
[TD]D[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]
Code:
Sub balance()
Dim VARvaloldestP As Integer
Dim VARvaloldestG As Integer
Dim VARvalREST As Integer
Dim VARvalcurrD As Integer
Dim VARbalP As Integer
Dim VARbalG As Integer
Dim VARarraydateP As Variant
Dim VARarraydateG As Variant
Dim x As Integer
Dim wbk As Workbook
Dim wsRD As Worksheet
Set wbk = ThisWorkbook
Set wsRD = wbk.Sheets("RD")
'Initialisation
'at R0:
VARvaloldestP = 0 'to store the oldest amount corresponding to P
VARvaloldestG = 0 'to store the oldest amount corresponding to G
VARvalREST = 0 'to keep looping until the rest is 0 or negative
VARvalcurrD = 0 'to store the current line value of the debit
VARbalP = 0 'to store the balance of P point at anytime
VARbalG = 0 'to store the balance of G poinr at anytime
Prow = 1
Grow = 1
ReDim VARarraydateP(Prow, 2) 'starting with one available spot
ReDim VARarraydateG(Grow, 2) 'starting with one available spot
'Table length
x = wsRD.Range(Cells(2, 1), Cells(2, 1).End(xlDown)).Rows.Count
For eachLineCheck = 1 To x
'at R1:
If Cells(1 + eachLineCheck, 3).Value = "P" Then 'If R1C3 = "P" Then
VARbalP = VARbalP + Cells(1 + eachLineCheck, 2) 'increase the balance of purchased points ==> balP = balP + R1C2
Prow = Prow + 1
ReDim VARarraydateP(Prow, 2) '> resize by 1 VARarraydateP(1+1:2), hence VARarraydateP=(2:1)
VARarraydateP(Prow, 1) = Cells(1 + eachLineCheck, 1).Value '> add the date in RxC3 @ VARarraydateP(1:1) 'here there is one P
VARarraydateP(Prow, 2) = Cells(1 + eachLineCheck, 2).Value '> add the amount in RxC2 @ VARarraydateP(1:2)
'checking by printing a table on the right
Cells(1 + Prow, 10).Value = VARarraydateP(Prow, 1)
Cells(1 + Prow, 11).Value = VARarraydateP(Prow, 2)
'Purchased point Balance
Cells(1 + eachLineCheck, 4).FormulaR1C1 = VARbalP
Cells(1 + eachLineCheck, 5) = VARbalG
Cells(1 + eachLineCheck, 6).Value = Application.Min(VARarraydateG)
Cells(1 + eachLineCheck, 7).Value = Application.Min(VARarraydateP)
Cells(1 + eachLineCheck, 8).Value = LBound(VARarraydateP, 1)
MsgBox VARarraydateP(Prow, 2)
'> here add in the array P of cumulative values the date
'> VARcountvalinarraydateP count the number of line in VARarraydateP
ElseIf Cells(1 + eachLineCheck, 3).Value = "G" Then 'If R1C3 = "G" Then
VARbalG = VARbalG + Cells(1 + eachLineCheck, 2) 'increase the balance of gifted points ==> balG = balG + R1C2
Grow = Grow + 1
ReDim VARarraydateG(Grow, 2) '> resize by 1 VARarraydateG(1+1:2), hence VARarraydateG=(2:1)
VARarraydateG(Grow, 1) = Cells(1 + eachLineCheck, 1).Value '> add the date in RxC3 @ VARarraydateG(1:1) 'here there is one G
VARarraydateG(Grow, 2) = Cells(1 + eachLineCheck, 2).Value '> add the amount in RxC2 @ VARarraydateG(1:2)
'checking by printing a table on the right
Cells(1 + Grow, 20).Value = VARarraydateG(Grow, 1)
Cells(1 + Grow, 21).Value = VARarraydateG(Grow, 2)
'Gifted point Balance
Cells(1 + eachLineCheck, 5).FormulaR1C1 = VARbalG
Cells(1 + eachLineCheck, 4) = VARbalP
Cells(1 + eachLineCheck, 6).Value = Application.Min(VARarraydateG)
Cells(1 + eachLineCheck, 7).Value = Application.Min(VARarraydateP)
Cells(1 + eachLineCheck, 8).Value = LBound(VARarraydateG, 2)
End If
Next eachLineCheck
' ElseIf Cells(1 + x, 3) = "D" Then 'If R1C3 = "D" Then
'
'
' While VARvalREST <> 0 ' WHILE >>>> while my VARvalREST is not equal to 0" ' need to check the actual line amount compared with the oldest usable wallet point amount
' 'check is the oldest point is a P or a G
'
'
' If LBound(VARarraydateP, 2) < LBound(VARarraydateG, 2) Then
' VARvalcurrD = R1C2.Value
' VARvaloldestP = Lbound VARarraydate(col2)
' if VARvalcurrD < VARvaloldestP
' balP = balP + R1C2
' rerange VARarraydateP range from (1:1) to (1:1) ' 2 lines and swip down to resize little by little // i can also use a counting number to swip down the camera rather than rewriting
'
'
'
' elseif Lbound arraydateG < Lbound arraydateP then
' balP = balP + R1C2
' redim arraydateP range from (1:1) to (0:1)
'
'
' End If
' End If
'
' Print bal of P VARbalP in R1C4
' Print bal of G VARbalG in R1C5
'
'
'
'
'From x = 1 to N
' at Rx:
' If RxC3 = "P" Then
' 'increase the balance of purchased points
' balP = balP + RxC2
' Print balP in RxC4
' Print balG in RxC5
'
' 'here add in the array P of cumulative values the date
' VARcountvalinarraydateP 'count the number of line in VARarraydateP
' resize by 1 VARarraydateP(1+1:2), hence VARarraydateP=(2:2) 'resize expanding the array by one to enable a spot
' add the date in RxC3 @ VARarraydateP(1:1) 'here there is one P
' add the amount in RxC2 @ VARarraydateP(1:2)
'
'
' If RxC3 = "G" Then
' 'increase the balance of gifted points
' balG = balG + RxC2
'
' 'here add in the array P of cumulative values the date
' VARcountvalinarraydateG 'count the number of line in VARarraydateG
' resize by 1 VARarraydateG(1+1:2), hence VARarraydateP=(2:1) 'resize expanding the array by one to enable a spot
' add the date in RxC3 @ VARarraydateG(1:1) 'here there is one G
' add the amount in RxC2 @ VARarraydateG(1:2)
'
' If RxC3 = "D" Then
' VARvalcurrD = RxC2.Value
'
' WHILE >>>> while my VARvalREST is not equal to 0" ' need to check the actual line amount compared with the oldest usable wallet point amount
'
' 'check is the oldest point is a P or a G
' if Lbound VARarraydateP(col1) < Lbound VARarraydateG(col1) then
' VARvaloldestP = Lbound VARarraydate(col2)
'
' if VARvalcurrD < VARvaloldestP
' balP = balP + RxC2
' redim/rerange VARarraydateP range from (1:1) to (1:1) ' 2 lines and swip down to resize little by little // i can also use a counting number to swip down the camera rather than rewriting
'
' elseif VARvalcurrD > VARvaloldestP
' balP = 0
' VARvalREST = -(balP + RxC2)
' redim/rerange VARarraydateP range from (1:1) to (1:1) ' 2 lines and swip down to resize little by little // i can also use a counting number to swip down the camera rather than rewriting
' End If
'
'
'
'
'
'
'
' elseif Lbound arraydateG < Lbound arraydateP then
' balP = balP + R1C2
' redim arraydateP range from (1:1) to (0:1)
'
'
' End If
' End If
' End If
'Next I
' Print bal of P VARbalP in R1C4
' Print bal of G VARbalG in R1C5
End Sub
Last edited: