Sub FillF5()
Dim LargeTemp2 As Double, Cnt2 As Integer, Cnt3 As Integer
Dim LargeTemp As Double, Cnt As Integer, Temptot As Double
Dim TotTax As Double, Lastrow As Integer, Counter As Integer
Dim Cnt2Temp As Integer, CntTemp As Integer, RemTot As Double
Dim RemGrs As Double, Cnt4 As Integer, CellCnt As Integer
Dim Cnt5 As Integer, Temp As Double
'fee in Sheets("Sheet2").Range("A" & 1).Value
'"G" has current balance of untraded amount
'"I" has total trade fee
' "J" has # of trades
'include all accts
With Sheets("Sheet1")
Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
'clear previous results
'*******Caution clears E7:j22 and G25:J & lastrow
Sheets("Sheet1").Range("G25:J" & Lastrow).ClearContents
Sheets("Sheet1").Range("E7:J22").ClearContents
'transfer acct total to "G" for current balance
For Cnt2 = 25 To Lastrow
Sheets("Sheet1").Range("G" & Cnt2).Value = Sheets("Sheet1").Range("C" & Cnt2).Value
Next Cnt2
'gross amts
For Cnt3 = 7 To 22
Sheets("Sheet1").Range("E" & Cnt3).Value = Sheets("Sheet1").Range("A" & Cnt3).Value * _
Sheets("Sheet1").Range("B" & 1).Value
Next Cnt3
For Counter = 1 To 2
Select Case Counter
'taxable/tax deferred
Case 1: Temptot = _
Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range("B25:B" & Lastrow), _
Worksheets("Sheet2").Range("A30"), Worksheets("Sheet1").Range("G25:G" & Lastrow))
Case 2: Temptot = _
Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range("B25:B" & Lastrow), _
Worksheets("Sheet2").Range("A31"), Worksheets("Sheet1").Range("G25:G" & Lastrow))
End Select
Do While Temptot > Sheets("Sheet2").Range("A" & 1).Value '1
LargeTemp = 0
LargeTemp2 = 0
If Counter = 1 Then
'current balance taxable
For Cnt2 = 25 To Lastrow
If Sheets("Sheet1").Range("B" & Cnt2).Value = "Taxable" Then
If LargeTemp2 < Sheets("Sheet1").Range("G" & Cnt2).Value Then
LargeTemp2 = Sheets("Sheet1").Range("G" & Cnt2).Value
Cnt2Temp = Cnt2
End If
End If
Next Cnt2
'grs amounts taxable
For Cnt = 7 To 22
If Sheets("Sheet1").Range("F" & Cnt).Value = "" And _
Sheets("Sheet1").Range("C" & Cnt).Value = "Taxable" Then
If LargeTemp < Sheets("Sheet1").Range("e" & Cnt).Value Then
Temp = Sheets("Sheet1").Range("e" & Cnt).Value
If LargeTemp2 >= Temp Then
LargeTemp = Sheets("Sheet1").Range("e" & Cnt).Value
End If
CntTemp = Cnt
End If
End If
Next Cnt
'current balance tax defer
Else
For Cnt2 = 25 To Lastrow
If Sheets("Sheet1").Range("B" & Cnt2).Value = "Tax Deferred" Then
If LargeTemp2 < Sheets("Sheet1").Range("G" & Cnt2).Value Then
LargeTemp2 = Sheets("Sheet1").Range("G" & Cnt2).Value
Cnt2Temp = Cnt2
End If
End If
Next Cnt2
'grs amounts tax defer
For Cnt = 7 To 22
If Sheets("Sheet1").Range("F" & Cnt).Value = "" And _
Sheets("Sheet1").Range("C" & Cnt).Value = "Tax Deferred" Then
If LargeTemp < Sheets("Sheet1").Range("e" & Cnt).Value Then
Temp = Sheets("Sheet1").Range("e" & Cnt).Value
If LargeTemp2 >= Temp Then
LargeTemp = Sheets("Sheet1").Range("e" & Cnt).Value
End If
CntTemp = Cnt
End If
End If
Next Cnt
End If
If LargeTemp = 0 Or LargeTemp2 - LargeTemp <= 0 Then
Exit Do
End If
'insert acct #
Sheets("Sheet1").Range("F" & CntTemp).Value = Sheets("Sheet1").Range("A" & Cnt2Temp).Value
If CntTemp <> 22 Then 'no fee for #22
'total fee (fee amt in Sheets("Sheet2").Range("A" & 1).Value)
Sheets("Sheet1").Range("I" & Cnt2Temp).Value = _
Sheets("Sheet1").Range("I" & Cnt2Temp).Value + Sheets("Sheet2").Range("A" & 1).Value
'net trade amt
Sheets("Sheet1").Range("e" & CntTemp).Value = Sheets("Sheet1").Range("e" & CntTemp).Value - _
Sheets("Sheet2").Range("A" & 1).Value
End If
'current balnce
Sheets("Sheet1").Range("G" & Cnt2Temp).Value = LargeTemp2 - LargeTemp
'trade cnter
Sheets("Sheet1").Range("j" & Cnt2Temp).Value = _
Sheets("Sheet1").Range("j" & Cnt2Temp).Value + 1
Temptot = Temptot - LargeTemp
MsgBox "View"
Loop
Next Counter
'********multi trade taxable/tax defer
For Counter = 1 To 2
Select Case Counter
Case 1: RemTot = _
Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range("B25:B" & Lastrow), _
Worksheets("Sheet2").Range("A30"), Worksheets("Sheet1").Range("G25:G" & Lastrow))
Case 2: RemTot = _
Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range("B25:B" & Lastrow), _
Worksheets("Sheet2").Range("A31"), Worksheets("Sheet1").Range("G25:G" & Lastrow))
End Select
Do While RemTot > Sheets("Sheet2").Range("A" & 1).Value '1
RemGrs = 0
For Cnt4 = 7 To 22
'tot grs taxable remaining
If Counter = 1 Then
If Sheets("Sheet1").Range("C" & Cnt4).Value = "Taxable" Then
If Sheets("Sheet1").Range("F" & Cnt4).Value = "" Then
RemGrs = Sheets("Sheet1").Range("E" & Cnt4).Value
Exit For
End If
End If
'tot grs tax defer remaining
Else
If Sheets("Sheet1").Range("C" & Cnt4).Value = "Tax Deferred" Then
If Sheets("Sheet1").Range("F" & Cnt4).Value = "" Then
RemGrs = Sheets("Sheet1").Range("E" & Cnt4).Value
Exit For
End If
End If
End If
Next Cnt4
If RemGrs = 0 Then
Exit Do
End If
CellCnt = 6
For Cnt5 = 25 To Lastrow
If Sheets("Sheet1").Range("G" & Cnt5).Value > 0 Then
If Sheets("Sheet1").Range("C" & Cnt4).Value = Sheets("Sheet1").Range("B" & Cnt5).Value Then
If Sheets("Sheet1").Range("G" & Cnt5).Value <= RemGrs Then
If Cnt4 <> 22 Then 'no fee for #22
'total fee
Sheets("Sheet1").Range("I" & Cnt5).Value = _
Sheets("Sheet1").Range("I" & Cnt5).Value + Sheets("Sheet2").Range("A" & 1).Value
If Sheets("Sheet1").Range("e" & Cnt4).Value >= Sheets("Sheet1").Range("G" & Cnt5).Value Then
'net remaining
Sheets("Sheet1").Range("e" & Cnt4).Value = Sheets("Sheet1").Range("e" & Cnt4).Value - _
Sheets("Sheet1").Range("G" & Cnt5).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = Sheets("Sheet1").Range("A" & Cnt5).Value & _
"(" & Sheets("Sheet1").Range("G" & Cnt5).Value - Sheets("Sheet2").Range("A" & 1).Value & ")"
RemTot = RemTot - Sheets("Sheet1").Range("g" & Cnt5).Value
Sheets("Sheet1").Range("G" & Cnt5).Value = 0
Else
'current balance remaining
Sheets("Sheet1").Range("G" & Cnt5).Value = Sheets("Sheet1").Range("G" & Cnt5).Value - _
Sheets("Sheet1").Range("e" & Cnt4).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = Sheets("Sheet1").Range("A" & Cnt5).Value & _
"(" & Sheets("Sheet1").Range("e" & Cnt4).Value - Sheets("Sheet2").Range("A" & 1).Value & ")"
RemTot = RemTot - Sheets("Sheet1").Range("e" & Cnt4).Value
Sheets("Sheet1").Range("e" & Cnt4).Value = 0
End If
Else
If Sheets("Sheet1").Range("e" & Cnt4).Value > Sheets("Sheet1").Range("G" & Cnt5).Value Then
'net remaining
Sheets("Sheet1").Range("e" & Cnt4).Value = Sheets("Sheet1").Range("e" & Cnt4).Value - _
Sheets("Sheet1").Range("G" & Cnt5).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = Sheets("Sheet1").Range("A" & Cnt5).Value & _
"(" & Sheets("Sheet1").Range("G" & Cnt5).Value & ")"
RemTot = RemTot - Sheets("Sheet1").Range("G" & Cnt5).Value
Sheets("Sheet1").Range("G" & Cnt5).Value = 0
Else
'current balance remaining
Sheets("Sheet1").Range("G" & Cnt5).Value = Sheets("Sheet1").Range("G" & Cnt5).Value - _
Sheets("Sheet1").Range("e" & Cnt4).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = Sheets("Sheet1").Range("A" & Cnt5).Value & _
"(" & Sheets("Sheet1").Range("e" & Cnt4).Value & ")"
RemTot = RemTot - Sheets("Sheet1").Range("e" & Cnt4).Value
Sheets("Sheet1").Range("e" & Cnt4).Value = 0
End If
End If
CellCnt = CellCnt + 1
MsgBox "View"
'trade cnt
Sheets("Sheet1").Range("j" & Cnt5).Value = _
Sheets("Sheet1").Range("j" & Cnt5).Value + 1
End If
End If
End If
Next Cnt5
If Sheets("Sheet1").Range("e" & Cnt4).Value = 0 Then
Sheets("Sheet1").Range("e" & Cnt4).Value = "MultiTrade"
End If
Loop
Next Counter
'multi allocate to both
For Cnt4 = 7 To 22
If Sheets("Sheet1").Range("F" & Cnt4).Value = "" Then
RemGrs = Sheets("Sheet1").Range("E" & Cnt4).Value
Exit For
End If
Next Cnt4
If Application.WorksheetFunction.Sum(Worksheets("Sheet1").Range("G25:G" & Lastrow)) <= _
Sheets("Sheet1").Range("E" & Cnt4).Value Then
CellCnt = 6
For Cnt5 = 25 To Lastrow
'trade cnt
Sheets("Sheet1").Range("j" & Cnt5).Value = _
Sheets("Sheet1").Range("j" & Cnt5).Value + 1
'net remaining
Sheets("Sheet1").Range("e" & Cnt4).Value = Sheets("Sheet1").Range("e" & Cnt4).Value - _
Sheets("Sheet1").Range("G" & Cnt5).Value
If Cnt4 <> 22 Then 'no fee for #22
'trade fee
Sheets("Sheet1").Range("I" & Cnt5).Value = _
Sheets("Sheet1").Range("I" & Cnt5).Value + Sheets("Sheet2").Range("A" & 1).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = Sheets("Sheet1").Range("A" & Cnt5).Value & _
"(" & Sheets("Sheet1").Range("G" & Cnt5).Value - Sheets("Sheet2").Range("A" & 1).Value & ")"
Else
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = Sheets("Sheet1").Range("A" & Cnt5).Value & _
"(" & Sheets("Sheet1").Range("G" & Cnt5).Value & ")"
End If
Sheets("Sheet1").Range("G" & Cnt5).Value = 0
CellCnt = CellCnt + 1
MsgBox "View"
Next Cnt5
If Sheets("Sheet1").Range("e" & Cnt4).Value = 0 Then
Sheets("Sheet1").Range("e" & Cnt4).Value = "MultiTrade"
End If
End If
End Sub