Automate net asset value (NAV) calculation on your stock portfolio (vba) in excel
Introduction
In this post I am creating a spreadsheet that will calculate stock portfolio performance. To do this I am calculating net asset value (NAV).
Net asset value is the value of each stock and the account balance summed, calculated each day as the stock prices fluctuate.
When a deposition or withdrawal is made you add or subtract NAV units to your stock portfolio. More about deposits, withdrawals and NAV units in an upcoming post.
All other transactions affects your stock portfolio performance and is added or subtracted from your account balance.
- Stock prices
- Dividends
- Commissions
- Interest payed or earned
In this excel tutorial I will only buy or sell stocks and calculate each stock's market value (column J) each day. Dates are in column A.
Transactions sheet
Here is the "Transactions" sheet. It contains some random stock data. Here I calculate stock portfolio market value each day.
Formula in H7:
Formula in I7:
NAV calculation sheet
In this sheet I calculate stock portfolio market value for each day. Excel queries yahoo and copies close price for each stock into E7 and down. Market value is calculated in F7 and down and the total is calculated in C4. The total is then copied into sheet "Transactions".
Formula in F3:
Formula in C3:
Formula in C4:
Formula in B7:
Copy cell and paste down as far as needed.
Formula in C7:
Copy cell and paste down as far as needed.
Formula in D7:
Copy cell and paste down as far as needed.
Formula in F7:
Copy cell and paste down as far as needed.
Web Query sheet
I created a web query in cell A5 on sheet "Web query".
http://table.finance.yahoo.com/table.csv?a=["m","m"]&b=["d","d"]&c=["y","y"]&d=["m","m"]&e=["d","d"]&f=["y","y"]&s=["ticker", "ticker"]&y=0&g=d&ignore=.csv
Parameters
y = cell P2
m = cell P3
d = cell P4
ticker = cell P5
Formula in P1:
Formula in P2:
Formula in P3:
Formula in P4:
VBA code:
Sub Calc_nav()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim rng As Range
Dim price As Range
Dim dest As Range
Dim mnt As Range
Dim i As Integer
Dim qryTblStocks As QueryTable
'Iterate rows
i = Worksheets("Nav calc").Range("F2")
Set mnt = Worksheets("Transactions").Range("J7")
Do While i <= Worksheets("Nav calc").Range("F3")
Worksheets("NAV calc").Range("C2").Value = i
'Iterate stock symbols
Set rng = Worksheets("NAV calc").Range("C7")
Set price = Worksheets("NAV calc").Range("E7")
Set dest = Worksheets("Web query").Range("P5")
Do While rng <> ""
dest.Value = rng.Value
'Refresh web query
Set qryTblStocks = ThisWorkbook.Worksheets("Web query").QueryTables(1)
With qryTblStocks
.Refresh BackgroundQuery:=False
End With
'Text to columns
Sheets("Web query").Select
Range("A5:A6").Select
Selection.Texttocolumns Destination:=Range("B5"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), _
Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
'Copy close value Web query F6 to NAV calc E7
price.Value = Worksheets("Web query").Range("F6")
Sheets("Web query").Select
Range("A5:A6").Select
Selection.ClearContents
'Iterate to next cells
Set rng = rng.Offset(1, 0)
Set price = price.Offset(1, 0)
Loop
mnt.Value = Worksheets("NAV calc").Range("C4")
Application.ScreenUpdating = True
Sheets("Transactions").Select
Application.ScreenUpdating = False
Set mnt = mnt.Offset(1, 0)
i = i + 1
Loop
End SubHow to use this spreadsheet
Download excel spreadsheet: Track your stock portfolio performance.xls Remember enabling macros.
Click "update" button on transactions sheet. Watch market values being calculated in column J.
Here is a picture of transactions sheet when NAV have been calculated.
Final notes
Make sure Transactions sheet is sorted by date.
Future improvements
Instead of making multiple web queries to yahoo, a better strategy would be to identify the maximum date range and then do one query for each stock in portfolio. This would also speed things up considerably.









February 16th, 2011 at 5:38 am
How are you tracking option buys and sells, also how are you dealing with short positions?
February 16th, 2011 at 8:50 am
Matt,
This excel sheet downloads price quotes from yahoo and it doesn´t track neither shorts nor options.
March 10th, 2011 at 9:48 am
Can the sheet be used for non US stock?
March 10th, 2011 at 10:37 am
Buzz,
Yes, if the stock has a ticker symbol in yahoo.