Urgent help with debugging a dictionary code used to calculate profit in stocks

potterotter

New Member
Joined
Jun 1, 2014
Messages
3
I essentially want to calculate the profit/loss for each stock in column L.

I want to use dictionaries to accomplish this.

Essentially I was given data in column A,B,C, D, L and M.
I calculated column N using another macro as a checker.

I want to calculate the profit for the stocks in column L and place it in column O.
Column M (Last trade) is the last traded price of those stocks. Column C (Price) is the price at which the stock was bought.

The significance of column B is whether you are Buying (B) or Selling Short (S) a stock. If you are buying a stock then if the last traded price (Column M) is higher than the price bought (Column C) then you would make a profit. Conversely, if you were selling short a stock and the last trade price (M) is HIGHER than the price bought (C) you would LOSE money.

Sorry I am new to VBA and dictionaries so any help I can get will be much appreciated!

Does anyone know what's wrong with my code?
Thanks!

I'm new to this forum so please let me know if I'm breaking any rules!

Code:
[/COLOR]Sub usingdictionarytocalc()


Dim stockdic As Dictionary
Dim pricedic As Dictionary
Dim quantitydic As Dictionary
Dim r As Integer
Dim i As Integer
Dim stock As Variant
Dim direction As Variant
Dim price As Double
Dim quantity As Double
Dim grossconsideration As Double
Dim symbol As Variant
Dim totalnet As Double
Dim profit As Double


'set main dictionary to contain all other dictionaries
    Set stockdic = New Dictionary
'set price dic
    Set pricedic = New Dictionary
'set quantity dic
    Set quantitydic = New Dictionary
    
    r = 2
    
    stock = ActiveSheet.Cells(r, 1)
    direction = ActiveSheet.Cells(r, 2)
    price = ActiveSheet.Cells(r, 3)
    quantity = ActiveSheet.Cells(r, 4)
    
'keep processing until we run out of stock
    While Len(stock) > 0
'find out if there is a stock dictionary with the price already existing and if so, grab it
        If stockdic.Exists(stock) Then
            If direction = "B" Then
            grossconsideration = grossconsideration + (ActiveSheet.Cells(r, 3).Value * ActiveSheet.Cells(r, 4).Value)
            Else
            grossconsideration = grossconsideration - (ActiveSheet.Cells(r, 3).Value * ActiveSheet.Cells(r, 4).Value)
            stockdic(stock) = grossconsideration
            End If
        Else
'if its a new product then add it into the amount
            If direction = "B" Then
            grossconsideration = grossconsideration + (ActiveSheet.Cells(r, 3).Value * ActiveSheet.Cells(r, 4).Value)
            Else
            grossconsideration = grossconsideration - (ActiveSheet.Cells(r, 3).Value * ActiveSheet.Cells(r, 4).Value)
            stockdic.Add stock, grossconsideration
            End If
        End If
'create a quantity dictionary to track how much net stock we have to calculate profit later
        If quantitydic.Exists(stock) Then
            If direction = "B" Then
            quantity = quantity + ActiveSheet.Cells(r, 4)
            Else
            quantity = quantity - ActiveSheet.Cells(r, 4)
            End If
        Else
            If direction = "B" Then
            quantity = quantity + ActiveSheet.Cells(r, 4)
            Else
            quantity = quantity - ActiveSheet.Cells(r, 4)
            quantitydic.Add stock, quantity
            End If
        End If
        
            r = r + 1
            stock = ActiveSheet.Cells(r, 1)
            direction = ActiveSheet.Cells(r, 2)
            price = ActiveSheet.Cells(r, 3)
            quantity = ActiveSheet.Cells(r, 4)
            
    Wend
    
    'MsgBox stockdic("ABH")
    
    
    
'now to calculate profit
    i = 2


    While Len(symbol) > 0
        
'bring out the net quantity associated with the symbol
        totalnet = quantitydic(ActiveSheet.Cells(i, 12)) * ActiveSheet.Cells(i, 13)
        profit = totalnet - pricedic(ActiveSheet.Cells(i, 12))
        ActiveSheet.Cells(i, 15).Value = profit
        
        i = i + 1
     
    Wend
    




End Sub

This is a sample of what the data looks like because I can't seem to attach files here.

SYMBOLDIRECTIONPRICEQTY SymbolLast TradePNL
VRSB1.9453485ABH15.92-315893.7026
WPPS9.5254249 BZ8.29764378.6152
BIPB28.6243796BIP28.3-232842.2168
VRSB2.2265725BKI33.785612134.609
NPB25.0329841CLW39.49-386356.1992
MWSB10.9664095DEL69.8912163310.95
MWSB10.4532003UFS91.33-456404.2553
BIPB28.8958415FBR8.92606513.6633
FBRS9.7814394GLT15.8-256974.1589
IPS32.3248051 IP31.473462081.518
BZS8.9019335MWS10.45-366544.5377
UFSB91.6526431 KS18.071088408.453
BZS9.0921222 NP24.81-227964.5874
WPPS9.5319116SPP3.41481091.4638
CLWB39.7827476VRS1.38-351248.9838
BIPB28.6023272WPP8.821254107.793
CLWB39.5618560
ABHB16.8525125
BZS8.952124
BIPB28.8345833
DELS70.2685303
IPS32.1658421
FBRS9.2789090
KSS18.7420368
FBRS9.7117615
UFSB91.547873
MWSB11.2185331
FBRS9.5094035
KSS18.6339808
VRSB1.4515598
MWSB10.9418878
WPPS8.8769795
WPPS9.1092038
UFSB91.9347684
MWSB10.9319538
ABHB16.4770721
FBRS9.1531790

<tbody>
</tbody>
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Upvote 0

Forum statistics

Threads
1,215,754
Messages
6,126,681
Members
449,328
Latest member
easperhe29

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