Tuesday, September 11, 2007

finally, redid this

This took me a long time to recreate. But, I think it's finally ok.
Sub Lines(tfield, tname2, tbroker2, tshares2, tbasis2, xshares, xbasis, ten())'----------------Lines----------------
dim clrw(3)
dim tots(9)'running totals

tshares1 = val(ten(0))'shs added to tname1, may be negative
tbasis1 = val(ten(9))'basis added to tname1, may be negative
if ten(6) = "exchange" or ten(6) = "transfer" then tbasis1 = 0
'for transfer and exchange, tbasis1 and tbasis2 start off at zero
'tbasis1 = - tbasis2 are calculated here

if val(tshares2) <> 0 then two = -1 else two = 0

if tfield = "stock" then
trange = "Trades"
trange2 = "CTrades"
tsheet = "Stock"
diff1 = tshares1 - int(tshares1)
diff2 = tshares2 - int(tshares2)
if ten(6) = "exchange" or ten(6) = "merger" then diff1 = 0 : diff2 = 0
elseif tfield = "fund" then
trange = "Fund"
trange2 = "CFund"
Call NewSheet(tsheet, ten(1), -1, -1)
oCell = thiscomponent.sheets.getbyname(tsheet).getcellbyposition(0,0)
thiscomponent.getCurrentController.select(oCell)
Call Resized(trange, tsheet, "shares", 0, 1, 1)
Call Resized(trange2, tsheet, "shares", 0, 2, 1)

diff1 = 0
diff2 = 0
else
' msgbox tfield
exit sub
end if

oSheet = SheetObj(trange,tsheet,clrw())
oRange2 = oSheet.getcellrangebyname(trange2)
Call Sorting(trange,tsheet,8,1,2,-1,-1,-1,-1)

tots(0) = xshares
tots(1) = xbasis
tots(2) = tshares1 + xshares 'new share total
tots(3) = tbasis1 + xbasis 'new basis
tots(4) = diff1
if two then
tots(5) = tshares2
tots(6) = tbasis2 ' not needed or used
tots(7) = diff2
tots(8) = xbasis ' not needed or used
tots(9) = xshares
end if

n = 1
do
Next1:
r = ""
if tots(0) = 0 then exit do
if (ten(6) = "exchange" or ten(6) = "transfer") and rounding(tots(2) - tots(0),3) = 0 then exit do

answer = FindTriple(trange, tsheet, 1, r, ten(1), ten(8), 7, "", 0, n, -1)
if not answer then exit do

if ten(1) = tname2 and ten(8) = tbroker2 then n = n + 1

lshares = oSheet.getcellbyposition(0,r).value'shares in entry
lbasis = oSheet.getcellbyposition(9,r).value'basis in entry
temp1 = lshares
temp2 = lbasis
if lshares = 0 then goto Next1

if two then call CopyLine(trange, tsheet, 0, r-clrw(1), trange, tsheet, 0, r + 1-clrw(1), 0)


'deal with first line, the line with tname1 and tname2
if ten(6) = "exchange" or ten(6) = "transfer" then 'may not transfer all shares
if lshares > abs(tots(2)) then temp1 = lshares - abs(tots(2)) else temp1 = 0
temp2 = rounding(lbasis * temp1 / lshares, 3)
else
temp1 = rounding(lshares * tots(2) / tots(0), 3)
if tots(1) = 0 then
temp2 = 0
else
temp2 = rounding(lbasis * tots(3) / tots(1), 3)
end if
end if

todel = 0
if two then'copy line r to "C" stock/fund
if rounding(temp1,3) = 0 then todel = -1
if todel then 'not sure whether first few lines here are needed if line not deleted
call CopyLine(trange, tsheet, 0, r-clrw(1), trange2, tsheet, 0, 1, todel) 'may delete line
oSheet.getcellrangebyname(trange2).getcellbyposition(0,1).value = lshares - temp1
oSheet.getcellrangebyname(trange2).getcellbyposition(9,1).value = lbasis - temp2
if todel then r = r - 1
end if
end if

if not todel then'line r not deleted
oSheet.getcellbyposition(0,r).value = temp1
oSheet.getcellbyposition(9,r).value = temp2
n = n + 1
if tots(4) <> 0 and temp1 <> 0 then
temp3 = rounding(lshares * tots(4) / tots(0), 3)
call CopyLine(trange, tsheet, 0, r-clrw(1), trange, tsheet, 0, r + 1-clrw(1), 0)
n = n + 1
oSheet.getcellbyposition(0,r+1).value = temp3
oSheet.getcellbyposition(0,r).value = temp1 - temp3
oSheet.getcellbyposition(9,r+1).value = rounding(temp2 * temp3 / temp1,2)
oSheet.getcellbyposition(9,r).value = temp2 - rounding(temp2 * temp3 / temp1,2)
oSheet.getcellbyposition(6,r).string = ten(6)
oSheet.getcellbyposition(6,r+1).string = "frac." & ten(6)
tots(4) = tots(4) - temp3
r = r + 1
end if
end if

tots(0) = tots(0) - lshares
tots(1) = tots(1) - lbasis
tots(2) = tots(2) - temp1 + lshares
tots(3) = tots(3) - temp2 + lbasis

if two then
r = r + 1
if ten(5) <> 0 and tots(5) <> 0 and tots(9) <> 0 then
temp1 = rounding(lshares * tots(5) / tots(9), 3)
temp2 = lbasis - temp2
end if

oSheet.getcellbyposition(0,r).value = temp1
oSheet.getcellbyposition(9,r).value = temp2
oSheet.getcellbyposition(1,r).string = tname2
oSheet.getcellbyposition(8,r).string = tbroker2
if tots(7) <> 0 and temp1 <> 0 then
temp3 = rounding(lshares * tots(7) / tots(9), 3)
call CopyLine(trange, tsheet, 0, r-clrw(1), trange, tsheet, 0, r + 1-clrw(1), 0)
if ten(1) = tname2 and ten(8) = tbroker2 then n = n + 1
oSheet.getcellbyposition(0,r).value = temp3
oSheet.getcellbyposition(0,r+1).value = temp1 - temp3
oSheet.getcellbyposition(9,r).value = rounding(temp2 * temp3 / temp1,2)
oSheet.getcellbyposition(9,r+1).value = temp2 - rounding(temp2 * temp3 / temp1,2)
oSheet.getcellbyposition(6,r).string = "frac." & ten(6)
tots(7) = tots(7) - temp1 + temp3
end if
tots(8) = tots(8) - lbasis
tots(6) = tots(6) - temp2
tots(9) = tots(9) - lshares
tots(5) = tots(5) - temp1
end if

loop
if ten(6) = "transfer" or ten(6) = "exchange" then
tbasis2 = tots(3)
' msgbox tbasis2
' msgbox temp2
ten(9) = tots(6)
oRange = ThisComponent.Sheets.getByName("Data").getCellRangeByName("All")
thiscomponent.getcurrentcontroller.select(oRange)

oRange.getcellbyposition(9,1).value = ten(9)
' msgbox ten(9)
Call CopyLine("all", "Data", 0, 1, trange2, tsheet, 0, 1, 0)
end if
'msgbox tbasis2
'msgbox tshares2
'for j = 0 to ubound(tots)
'msgbox tots(j) & " " & j
'next

'change name of fund sheet
If tfield = "fund" And oSheet.name <> tname2 Then
temp = InputBox("What is the full name of the the ""new"" fund?" & _
Chr(10) & "OK to leave blank.")
if temp = "" then temp = tname2
oSheet.rows.insertbyindex(0,1)
oSheet.getcellbyposition(0,0).string = temp
oSheet.getcellbyposition(0,0).CharColor = 16711680 'red
oSheet.name = tname2
End If


End Sub

No comments: