Sub FillF8()
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, SplitStr As Variant, TickStr As String
'fee in Sheets("Sheet2").Range("A" & 1).Value
'"E25:etc" has current balance of untraded amount
'"F25:etc" has # of trades
'"G25:etc" has total trade fee
'exit if impossible (A23 is sum of % = 1)
If Sheets("Sheet1").Range("A" & 23).Value > 1 Then
MsgBox "More than %100 allocated. Exit!"
Exit Sub
End If
'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:F & lastrow
Sheets("Sheet1").Range("E25:G" & Lastrow).ClearContents
Sheets("Sheet1").Range("E7:J22").ClearContents
'transfer acct total to "E" for current balance
For Cnt2 = 25 To Lastrow
Sheets("Sheet1").Range("E" & Cnt2).Value = Sheets("Sheet1").Range("C" & Cnt2).Value
Next Cnt2
'gross amts
For Cnt3 = 7 To 22
If Sheets("Sheet1").Range("A" & Cnt3).Value <> "0%" Then
Sheets("Sheet1").Range("E" & Cnt3).Value = Format(Sheets("Sheet1").Range("A" & Cnt3).Value * _
Sheets("Sheet1").Range("B" & 1).Value, "currency")
Else
Sheets("Sheet1").Range("E" & Cnt3).Value = 0
End If
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("E25:E" & Lastrow))
Case 2: Temptot = _
Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range("B25:B" & Lastrow), _
Worksheets("Sheet2").Range("A31"), Worksheets("Sheet1").Range("E25:E" & Lastrow))
End Select
Do While Temptot > 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("E" & Cnt2).Value Then
LargeTemp2 = Sheets("Sheet1").Range("E" & 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 And _
LargeTemp2 >= Sheets("Sheet1").Range("e" & Cnt).Value Then
LargeTemp = Sheets("Sheet1").Range("e" & Cnt).Value
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("E" & Cnt2).Value Then
LargeTemp2 = Sheets("Sheet1").Range("E" & 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 And _
LargeTemp2 >= Sheets("Sheet1").Range("e" & Cnt).Value Then
LargeTemp = Sheets("Sheet1").Range("e" & Cnt).Value
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 = _
Format(Sheets("Sheet1").Range("A" & Cnt2Temp).Value, "000-000000")
If CntTemp <> 22 Then 'no fee for #22
'total fee (fee amt in Sheets("Sheet2").Range("A" & 1).Value)
Sheets("Sheet1").Range("G" & Cnt2Temp).Value = _
Sheets("Sheet1").Range("G" & Cnt2Temp).Value + Sheets("Sheet2").Range("A" & 1).Value
'net trade amt
Sheets("Sheet1").Range("e" & CntTemp).Value = _
Format(Sheets("Sheet1").Range("e" & CntTemp).Value - _
Sheets("Sheet2").Range("A" & 1).Value, "currency")
End If
'current balnce
Sheets("Sheet1").Range("E" & Cnt2Temp).Value = LargeTemp2 - LargeTemp
'trade cnter
Sheets("Sheet1").Range("F" & Cnt2Temp).Value = _
Sheets("Sheet1").Range("F" & 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("E25:E" & Lastrow)): _
Case 2: RemTot = _
Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range("B25:B" & Lastrow), _
Worksheets("Sheet2").Range("A31"), Worksheets("Sheet1").Range("E25:E" & Lastrow))
End Select
Do While RemTot > 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("E" & Cnt5).Value > 0 Then
If Sheets("Sheet1").Range("C" & Cnt4).Value = Sheets("Sheet1").Range("B" & Cnt5).Value Then
If Sheets("Sheet1").Range("E" & Cnt5).Value <= RemGrs Then
If Cnt4 <> 22 Then 'no fee for #22
'total fee
Sheets("Sheet1").Range("G" & Cnt5).Value = _
Sheets("Sheet1").Range("G" & Cnt5).Value + Sheets("Sheet2").Range("A" & 1).Value
If Sheets("Sheet1").Range("e" & Cnt4).Value >= Sheets("Sheet1").Range("E" & Cnt5).Value Then
'net remaining
Sheets("Sheet1").Range("e" & Cnt4).Value = Format(Sheets("Sheet1").Range("e" & Cnt4).Value - _
Sheets("Sheet1").Range("E" & Cnt5).Value, "currency")
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = _
Format(Sheets("Sheet1").Range("A" & Cnt5).Value, "000-000000") & _
"(" & Format(Sheets("Sheet1").Range("E" & Cnt5).Value - _
Sheets("Sheet2").Range("A" & 1).Value, "currency") & ")"
RemTot = RemTot - Sheets("Sheet1").Range("E" & Cnt5).Value
'trade cnt
Sheets("Sheet1").Range("F" & Cnt5).Value = _
Sheets("Sheet1").Range("F" & Cnt5).Value + 1
Sheets("Sheet1").Range("E" & Cnt5).Value = 0
Else
If Sheets("Sheet1").Range("e" & Cnt4).Value <> 0 Then
'current balance remaining
Sheets("Sheet1").Range("E" & Cnt5).Value = Sheets("Sheet1").Range("E" & Cnt5).Value - _
Sheets("Sheet1").Range("e" & Cnt4).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = _
Format(Sheets("Sheet1").Range("A" & Cnt5).Value, "000-000000") & _
"(" & Format(Sheets("Sheet1").Range("e" & Cnt4).Value - _
Sheets("Sheet2").Range("A" & 1).Value, "currency") & ")"
RemTot = RemTot - Sheets("Sheet1").Range("e" & Cnt4).Value
'trade cnt
Sheets("Sheet1").Range("F" & Cnt5).Value = _
Sheets("Sheet1").Range("F" & Cnt5).Value + 1
Sheets("Sheet1").Range("e" & Cnt4).Value = 0
End If
End If
Else
If Sheets("Sheet1").Range("e" & Cnt4).Value >= Sheets("Sheet1").Range("E" & Cnt5).Value Then
'net remaining
Sheets("Sheet1").Range("e" & Cnt4).Value = Format(Sheets("Sheet1").Range("e" & Cnt4).Value - _
Sheets("Sheet1").Range("E" & Cnt5).Value, "currency")
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = _
Format(Sheets("Sheet1").Range("A" & Cnt5).Value, "000-000000") & _
"(" & Format(Sheets("Sheet1").Range("E" & Cnt5).Value, "currency") & ")"
RemTot = RemTot - Sheets("Sheet1").Range("E" & Cnt5).Value
'trade cnt
Sheets("Sheet1").Range("F" & Cnt5).Value = _
Sheets("Sheet1").Range("F" & Cnt5).Value + 1
Sheets("Sheet1").Range("E" & Cnt5).Value = 0
Else
If Sheets("Sheet1").Range("e" & Cnt4).Value <> 0 Then
'current balance remaining
Sheets("Sheet1").Range("E" & Cnt5).Value = Sheets("Sheet1").Range("E" & Cnt5).Value - _
Sheets("Sheet1").Range("e" & Cnt4).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = _
Format(Sheets("Sheet1").Range("A" & Cnt5).Value, "000-000000") & _
"(" & Format(Sheets("Sheet1").Range("e" & Cnt4).Value, "currency") & ")"
RemTot = RemTot - Sheets("Sheet1").Range("e" & Cnt4).Value
'trade cnt
Sheets("Sheet1").Range("F" & Cnt5).Value = _
Sheets("Sheet1").Range("F" & Cnt5).Value + 1
Sheets("Sheet1").Range("e" & Cnt4).Value = 0
End If
End If
End If
'MsgBox "view"
CellCnt = CellCnt + 1
End If
End If
End If
Next Cnt5
If Sheets("Sheet1").Range("e" & Cnt4).Value < 0.1 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 = "" And _
Sheets("Sheet1").Range("C" & Cnt4).Value = "Allocate to Both*" Then
RemGrs = Sheets("Sheet1").Range("E" & Cnt4).Value
Exit For
End If
Next Cnt4
If Cnt4 = 23 Then
MsgBox "Allocation doesn't equal input!"
Exit Sub
End If
CellCnt = 6
For Cnt5 = 25 To Lastrow
If Sheets("Sheet1").Range("E" & Cnt5).Value <> 0 Then
'trade cnt
Sheets("Sheet1").Range("F" & Cnt5).Value = _
Sheets("Sheet1").Range("F" & Cnt5).Value + 1
'net remaining
Sheets("Sheet1").Range("e" & Cnt4).Value = Format(Sheets("Sheet1").Range("e" & Cnt4).Value - _
Sheets("Sheet1").Range("E" & Cnt5).Value, "currency")
On Error Resume Next
SplitStr = Split(Sheets("Sheet1").Range("D" & Cnt4).Value, "/")
If Sheets("Sheet1").Range("B" & Cnt5).Value = "Tax Deferred" Then
TickStr = SplitStr(0)
Else
TickStr = SplitStr(1)
End If
If Cnt4 <> 22 Then 'no fee for #22
'trade fee
Sheets("Sheet1").Range("G" & Cnt5).Value = _
Sheets("Sheet1").Range("G" & Cnt5).Value + Sheets("Sheet2").Range("A" & 1).Value
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = _
Format(Sheets("Sheet1").Range("A" & Cnt5).Value, "000-000000") & _
"(" & TickStr & ":" & Format(Sheets("Sheet1").Range("E" & Cnt5).Value _
- Sheets("Sheet2").Range("A" & 1).Value, "currency") & ")"
Else
'insert acct #
Sheets("Sheet1").Cells(Cnt4, CellCnt) = _
Format(Sheets("Sheet1").Range("A" & Cnt5).Value, "000-000000") & _
"(" & TickStr & ":" & Sheets("Sheet1").Range("E" & Cnt5).Value & ")"
End If
Sheets("Sheet1").Range("E" & Cnt5).Value = 0
CellCnt = CellCnt + 1
'MsgBox "View"
End If
Next Cnt5
If Sheets("Sheet1").Range("e" & Cnt4).Value < 0.1 Then
Sheets("Sheet1").Range("e" & Cnt4).Value = "MultiTrade"
End If
End Sub