Date | Type | Stock | Price | Quantity | Amount | VBA | Correct | |
04-01-2024 | Bought | MINM | 3,960 | 10 | 39,60 | |||
04-01-2024 | Sold | MINM | 3,900 | 10 | 39,00 | -0,60 | -0,60 | |
04-01-2024 | Bought | MINM | 4,060 | 10 | 40,60 | - | ||
04-01-2024 | Bought | MINM | 3,760 | 10 | 37,60 | - | ||
04-01-2024 | Sold | MINM | 4,130 | 20 | 82,60 | 7,40 | 4,40 | |
04-01-2024 | Bought | MINM | 4,220 | 5 | 21,10 | - | ||
04-01-2024 | Sold | MINM | 4,280 | 5 | 21,40 | 0,30 | 0,30 | |
04-01-2024 | Bought | MINM | 4,200 | 5 | 21,00 | - | ||
04-01-2024 | Bought | MINM | 3,920 | 5 | 19,60 | - | ||
04-01-2024 | Bought | MINM | 3,860 | 5 | 19,30 | - | ||
04-01-2024 | Bought | MINM | 3,840 | 5 | 19,20 | - | ||
04-01-2024 | Sold | MINM | 4,000 | 20 | 80,00 | 2,80 | 0,90 | |
04-01-2024 | Bought | MINM | 4,330 | 2 | 8,66 | - | ||
04-01-2024 | Sold | MINM | 4,460 | 2 | 8,92 | 1,24 | 0,26 | |
04-01-2024 | Bought | MINM | 4,490 | 2 | 8,98 | - | ||
04-01-2024 | Sold | MINM | 4,400 | 2 | 8,80 | 0,14 | -0,18 | |
04-01-2024 | Bought | MINM | 4,580 | 2 | 9,16 | - | ||
04-01-2024 | Sold | MINM | 4,630 | 2 | 9,26 | 0,28 | 0,10 | |
04-01-2024 | Bought | MINM | 5,500 | 3 | 16,50 | - | ||
04-01-2024 | Sold | MINM | 5,320 | 1 | 5,32 | 0,74 | -0,18 | |
04-01-2024 | Sold | MINM | 5,320 | 2 | 10,64 | -0,36 | -0,36 | |
04-01-2024 | Bought | MINM | 5,170 | 3 | 15,51 | - | ||
04-01-2024 | Sold | MINM | 5,080 | 3 | 15,24 | -0,27 | -0,27 | |
04-01-2024 | Bought | MINM | 5,270 | 5 | 26,35 | - | ||
04-01-2024 | Sold | MINM | 5,280 | 5 | 26,40 | 0,05 | 0,05 | |
04-01-2024 | Bought | MINM | 5,330 | 2 | 10,66 | - | ||
04-01-2024 | Sold | MINM | 5,140 | 2 | 10,28 | -0,38 | -0,38 | |
04-01-2024 | Bought | MINM | 5,185 | 2 | 10,37 | - | ||
04-01-2024 | Sold | MINM | 5,213 | 2 | 10,43 | 0,06 | 0,06 | |
04-01-2024 | Bought | OMGA | 5,588 | 1 | 5,59 | - | ||
04-01-2024 | Sold | OMGA | 5,596 | 1 | 5,60 | 0,01 | 0,01 |
I have this data and I want to calculate the Profit automatically. I have a code but it does not function when I have more than 1 buy or sell, I will put the code below, any help would be fantastic, thank you!
Sub CalculateProfit()
Dim lastRow As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("SheetName") ' Replace "YourSheetName" with the actual sheet name
' Find the last row with data in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Array to store matched transactions
Dim matchedTransactions() As Boolean
ReDim matchedTransactions(2 To lastRow) ' Assuming data starts from row 2
' Loop through the transactions in reverse order
For i = lastRow To 2 Step -1
If ws.Cells(i, 2).Value = "Sold" And Not matchedTransactions(i) Then ' Check if it's an unmatched sale transaction
Dim sellPrice As Double
Dim quantity As Integer
Dim buyPrice As Double
' Get the sell price and quantity from the sale transaction
sellPrice = ws.Cells(i, 4).Value
quantity = ws.Cells(i, 5).Value
' Find the first unmatched buy transaction for the same stock
For j = i - 1 To 2 Step -1
If ws.Cells(j, 3).Value = ws.Cells(i, 3).Value And ws.Cells(j, 2).Value = "Bought" And Not matchedTransactions(j) Then
buyPrice = ws.Cells(j, 4).Value
matchedTransactions(j) = True ' Mark the buy as matched
Exit For
End If
Next j
' Calculate profit and update the "Amount" column
If buyPrice <> 0 Then
Dim profit As Double
profit = (sellPrice - buyPrice) * quantity
ws.Cells(i, 7).Value = profit
End If
End If
Next i
End Sub