Hi Larry
I think you want something like this.
I have assumed that you are going to run this macro after all the values are already in the cells from your various formulas. What I did was to copy columns A to M to sheet1 and ran the code against that sheet - so you will need to amend the first line of code if you want to use a different sheet.
I was not sure if you wanted a formula in the cells being overwritten or values - I think you would probably prefer to see the formula - so that is what I have done.
The vba looks for Zero values in columns H to M and then looks for the first time that sell conditions are met and overwrites in correct cell in columns H to M
To make it a little easier to see what is going on with all those numbers, I have included 3 lines that you can remove once you are happy the code is doing what you want it to do.
- The zero value cells in rows H to M are highlighted and also cells amended by the next sell are highlighted in a different color.
- There is a comprehensive message box that informs you exactly where the values are coming from, so that it makes it easy to check the logic. The box will pop up every time a cell is being amended. This may be useful for the first few entries but very tedious after that. At the moment there is an apostrophe in front to stop it running. To switch it on just remove the apostrophe. When you get bored of seeing the messages you can hit ctrl + break to stop the code running. I found it very useful when I was testing the code.
One thing I have noticed is that that there are times when a previous cell overwrite is then overwritten again because the price on the day satisfies the conditions to trigger a sell more than once. If this is what you want the spreadsheet to do then that is fine. (It looks odd that these potential sells "disappear" - but I have treated them in the same manner as what we discussed yesterday.)
If anything is unclear or if you need any help amending anything please shout.
Any problems running the code and I could send your original workbook which now incudes the VBA - but I am sure you would rather have a go yourself first. I hope this helps you make a big profit an selling stock. /
Yon
Sub NextSell()
Sheets("Sheet1").Select
'set up variables
Dim Shares As String, Margin As String, CellRef As String
Dim r As Integer, c As Integer
For r = 4 To 255
For c = 8 To 13
If Cells(r, 2).Value = 0 Then Exit Sub 'ignore later cells with no purchases
If Cells(r, c) = 0 Then
' highlight cells where no sales
Cells(r, c).Interior.Color = RGB(250, 200, 150) 'Can delete this line
Price = Cells(r, 2).Value
MinSellPrice = Price + Cells(3, c).Value
For r2 = r + 1 To 255
If Cells(r2, 3) >= MinSellPrice Then
'highlight cells where condition is met for first time
Cells(r2, c).Interior.Color = RGB(200, 200, 200) ''Can delete this line
Shares = Cells(r, 7).Address
Margin = Cells(3, c).Address
'Can delete the next 4 lines
'MsgBox Format(Range(Shares).Value, "0,000") & " Shares" & vbNewLine _
& "Bought on " & Cells(r, 1).Value & vbNewLine & "Cost $" & Format(Price, "0.00") & vbNewLine & _
"Margin " & Range(Margin).Value & vbNewLine & "Minimum selling price $ " _
& Format(MinSellPrice, "0.00") & vbNewLine & "Achieved on " & Cells(r2, 1).Value
Cells(r2, c).Select
'put formula in cells when stock can be sold
CellRef = "=" & Shares & "*" & Margin
ActiveCell.Formula = CellRef
r2 = 255
Else
End If
Next r2
Else
End If
Next c
Next r
End Sub