Very Basic Stock Screening VBA Code -Essential for Stock Traders-

Trader11

New Member
Joined
Feb 25, 2011
Messages
25
Hello,

I need help on a VBA code -which I think is a very basic one.

I have cells that display, "BUY", "SELL" or "-" based on some conditions. I would like to create a VBA code so that a corresponding cell would show the date when the signals i.e. "BUY", "SELL" or "-" are triggered for the given cell. When the signal disappears (due to change in the linked condition) the date should disappear as well. As some might have guessed this is for Stock Screening hence I have some hundred rows.

Also I've saw threads that read, "copy this code and paste it to the screen where it says 'view code' and then it should run automatically", for example, but I'm struggling with that. Will I need to click on "Run" function? And how will I apply this code on the column I like.

Thank you so much in advance.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You could use the Worksheet_Calculate event but this may have drawbacks in that it may possibly trigger too often and/or have too many formula cells to evaluate and consequently impact on your sheet's performance.

Anyway, you could give this a try. Remove the other code and replace it with this.

Test in a copy of your workbook.

<font face=Courier New><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Calculate()<br>    <SPAN style="color:#00007F">Dim</SPAN> rFormulas <SPAN style="color:#00007F">As</SPAN> Range, rCel <SPAN style="color:#00007F">As</SPAN> Range<br><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rFormulas = Columns("Z").SpecialCells(xlCellTypeFormulas)<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> rFormulas <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> rCel <SPAN style="color:#00007F">In</SPAN> rFormulas<br>            <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> UCase(rCel.Value)<br>                <SPAN style="color:#00007F">Case</SPAN> "BUY", "SELL"<br>                    rCel.Offset(, 1).Value = Format(Date, "short date")<br>                Case <SPAN style="color:#00007F">Else</SPAN><br>                    rCel.Offset(, 1).ClearContents<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> rCel<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
 
Upvote 0
Thanks Peter,

The code does work, but it took more than 30 minutes to evaluate before I aborted. I have 15000 cells in total -though minority of them linked together.

Would you recommend any other code?

Thanks.
 
Upvote 0
Not sure, tell us more about ..
- What you have in your sheet,
- What the formula in column Z is,
- Are there any other cells (apart from the ones shown in the formula in column Z) that influence the column Z formula? That is, do any of the cells referenced by the col Z formula also contain formulas?
- How are the cells that influence the col Z formula changed? Also, how often are they changed and how many are changed at the same time?
 
Upvote 0
-I have around 9000 cells in total almost all are imported from Bloomberg Terminal (current price, volume etc.). The functions read like: =bdp(.....)
Data update daily.

-The formula in column Z is:
=IF(AND(N3<=30,Q3>0.02),"BUY",IF(AND(N3>=70,Q3<0.02),"SELL",""))

-As you can see, apart from Cells C and N, nothing influences any cell in column Z.

-Cells N and C are again imported from Bloomberg Terminal and update themselves once when I open the spreadsheet i.e. daily.
 
Upvote 0
Try this. Remove any previous worksheet change or worksheet calculate code and try this worksheet change code. It should be quicker but whether it will be fast enough remains to be seen.

I have assumed that you would manually format column AA with the date format you want so the code doesn't have to do that as well.

(It isn't relevant to my code but your descriptions about columns C and N don't seem to match you formula that clearly depends on columns Q and N)


<font face=Courier New><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> aFormulas, aResults<br>    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, num <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> tDay <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Date</SPAN><br><br>    tDay = <SPAN style="color:#00007F">Date</SPAN><br>    aFormulas = Range("Z3", Range("Z" & Rows.Count).End(xlUp)).Value<br>    num = <SPAN style="color:#00007F">UBound</SPAN>(aFormulas, 1)<br>    <SPAN style="color:#00007F">ReDim</SPAN> aResults(1 <SPAN style="color:#00007F">To</SPAN> num, 1 <SPAN style="color:#00007F">To</SPAN> 1)<br>    <SPAN style="color:#00007F">For</SPAN> r = 1 <SPAN style="color:#00007F">To</SPAN> num<br>        <SPAN style="color:#00007F">If</SPAN> Len(aFormulas(r, 1)) > 0 <SPAN style="color:#00007F">Then</SPAN><br>            aResults(r, 1) = tDay<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> r<br>    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>    Range("AA3").Resize(num).Value = aResults<br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
That C should be Q, my mistake.

It seems to work for now. Let me track this for couple days to check if everything works fine.

I'll write the results here.

Peter, thank you very much.
 
Upvote 0
Hi,

I opened the file today, the macro doesn't work. I still see the same recommendation with today's date (it should be the date when it first recommended me either buy or sell).

I also doubt if the recommendation and the date would dissappear, based on new conditions which change everyday.

Please advise, your help is very much appreciated.

Thanks
 
Upvote 0
I think we may have confusion between Excel's (and vba coder's) interpretation of 'change' in a worksheet and your interpretation of it. From reading your last posts I think this may be happening:

When you open the sheet on a new day the values in columns N & Q are entered, though many will appear the same. Excel (and the Worksheet_Change event) see all those cells as chaned because fresh (though maybe not different) values have been entered. You appear to be interested when the value in the cell changes to a different value from the previous value in the cell.

I've used an extra helper column to record existing values from column Z. When anything in columns N, Q changes, the formula in col Z re-evaluates. This new code should now only act on that row if the new value in column Z differes from the previous value (that is now stored in col AB.

As a starting point to test this new code, copy the whole of column Z and use Paste Special... Values to paste into column AB (in corresponding rows).

Then try this code. I'm still not sure it is exactly what you want but it might get it to where you can start trying things yourself a bit more.


<font face=Courier New><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> aFormulas, aResults, aOldData<br>    <SPAN style="color:#00007F">Dim</SPAN> LRN <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, LRZ <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, LRAB <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, num <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> tDay <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Date</SPAN><br>    <br>    <SPAN style="color:#00007F">Const</SPAN> FirstRw <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN> = 3<br><br>    tDay = <SPAN style="color:#00007F">Date</SPAN><br>    LRN = Range("N" & Rows.Count).End(xlUp).Row<br>    LRZ = Range("Z" & Rows.Count).End(xlUp).Row<br>    LRAB = Range("AB" & Rows.Count).End(xlUp).Row<br>    num = IIf(LRN > LRZ, LRN, LRZ)<br>    num = IIf(LRAB > num, LRAB, num) - FirstRw + 1<br>    aFormulas = Range("Z" & FirstRw).Resize(num).Value<br>    aResults = Range("AA" & FirstRw).Resize(num).Value<br>    aOldData = Range("AB" & FirstRw).Resize(num).Value<br>    <SPAN style="color:#00007F">For</SPAN> r = 1 <SPAN style="color:#00007F">To</SPAN> num<br>        <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> aFormulas(r, 1)<br>            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Is</SPAN> = aOldData(r, 1)<br>            <br>            <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN><br>                aResults(r, 1) = tDay<br>                aOldData(r, 1) = aFormulas(r, 1)<br>                <br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> r<br>    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>    Range("AA3").Resize(num).Value = aResults<br>    Range("AB3").Resize(num).Value = aOldData<br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Peter,

Thanks a lot again.

I'm checking the code but can't find "Q" in any rows. Column Q has also impact on Column Z.

But the code seems to consist only "N". Am I missing something?

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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