Venturing into BA's Excel world

Discussion regarding the spreadsheet functionality of Bet Angel.
User avatar
firlandsfarm
Posts: 2722
Joined: Sat May 03, 2014 8:20 am

Hi guys, thanks again for your support. I had a 'distraction' yesterday. :( I suddenly started to get a Bet Status of "ERROR" on one of the pair of ws for no apparent reason. All worked fine on Monday but having done a little 'tweaking' yesterday morning the problem occurred! Clearly something didn't like being tweaked. :(

I searched for the Status: ERROR problem in this Forum and found a couple of threads you took part in …

viewtopic.php?f=19&t=17715&p=172656&hil ... us#p172656
and
viewtopic.php?f=19&t=19651&p=200057&hil ... us#p200057

… and from your comments in those threads I decided to take the bet placing conditions out of the "L" cells, put them at the end of the BA rows (AF onwards) and rejig the bet placement control counters and that seems to have worked. :)
User avatar
firlandsfarm
Posts: 2722
Joined: Sat May 03, 2014 8:20 am

CallumPerry wrote:
Fri Nov 15, 2019 9:30 pm
As for making sure your formulae are in code, if you're not sure how to just click 'Record a Macro' and then select the cells that have formulae in (in order if you have chains) and press enter for EVERY cell. You'll see them appear in a new module which you can call from before your very first IF(value<0) line.
At first I thought maybe this post is off topic but having checked the topic title I think not … it's all part of learning about using Excel with BA and trying to get the best out of it from beginner stage. So in doing a little groundwork before trying to add the formulas as code I came across the Personal.XLSB file/facility and wondered if there any reason why you would not put all your macros in this workbook so that they are available for any wb except perhaps any that are very much specific to a particular wb.
CallumPerry
Posts: 575
Joined: Wed Apr 19, 2017 5:12 pm
Location: Wolverhampton

To be honest I think that's just an organisational choice. What matters most is that things make sense to you and are easy to follow, especially with a system you wish to build upon. I have a system which multiple sections, the parts specific to the sheets happen in the sheet code and additional parts I put into separate modules and rename them.

As you said, for a beginner, users just need to be able to understand how formulae can be altered into code and how it can be called from different areas. If you keep things neat, tidy and learn a few technical names (variable, loop, routine etc) you've made the big scary first jump. I wouldn't consider myself to be more than a low level intermediate level in all honesty, I just try to help where/when I can and enjoy my own learning journey.
User avatar
firlandsfarm
Posts: 2722
Joined: Sat May 03, 2014 8:20 am

CallumPerry wrote:
Wed Nov 20, 2019 3:04 pm
If you keep things neat, tidy and learn a few technical names (variable, loop, routine etc) you've made the big scary first jump.
It's not a scary jump for me Callum, As an ex BASIC coder I have no problems picturing If/Then's, For/Next's, While/Wend's, Arrays (though I think VBA addresses them differently), Subroutines and Procedures etc. (Interestingly what you now call a Subroutine we called a Procedure, Subroutines are (were) something different then!) and no problem understanding numeric, Boolean and string variables. The biggest problem I am having is seeing what runs all the time and what doesn't. I was rather expecting a reference to a cell value would monitor that cell value but not so.

When I tried my first attempt I was told it didn't work because there was nothing telling it to activate the code and that I needed something like Worksheet_Change to trigger it. OK, I understood that but I then discovered Worksheet_Calculate and thinking that may not be as aggressive as Worksheet_Change I composed my latest code (Master Sub). That works fine so Worksheet_Calculate is the activator. But having got to that stage with formulas in the cells and I then tried to speed things up a little by following your suggestion for the formulas to be in a subroutine in the VBA.

I labelled it Subroutine Formulas, put a 'call' for it in the Master Sub and … nothing! Yet if I use the assigned key for the macro it runs so again, just as my very first attempt, it's the activating of the Sub that's the problem. So how come Worksheet_Calculate works to monitor when the Status cells need clearing but not to calculate the formulas? I haven't yet tried to put the Formula code directly into Master Sub … I will try that tomorrow morning.
CallumPerry wrote:
Mon Nov 18, 2019 8:09 pm
Truthfully, I have never come across EnableEvents. From a quick google search there’s a quick explanation here: https://www.mrexcel.com/board/threads/a ... on.329759/
Use enable events, calculation and screenupdating. Anybody else used all three with larger systems or is this just being over the top?
I've had a look at that exchange on MrExcel and frankly it seems to conclude 'don't use EnableEvents'! He comments "You need to be careful with this if your macros depend on the values of formulas AND your code changes the values of the cells those formulas use." and the questioner concludes "Since my macros often include a need for these things to happen, it's probably best if I not use these two features." … mine likewise (and I would have thought most wb tasks) have values that rely on values so if I turn off calculating by whichever means the value that triggers the bet is not changed! I have turned off automatic updating on large ws in the past but mainly for data input when I haven't wanted an update after every cell entry. What actually happens when the Calculate in turned back on again?
CallumPerry
Posts: 575
Joined: Wed Apr 19, 2017 5:12 pm
Location: Wolverhampton

firlandsfarm wrote:
Wed Nov 20, 2019 6:28 pm
CallumPerry wrote:
Wed Nov 20, 2019 3:04 pm
If you keep things neat, tidy and learn a few technical names (variable, loop, routine etc) you've made the big scary first jump.
It's not a scary jump for me Callum, As an ex BASIC coder I have no problems picturing If/Then's, For/Next's, While/Wend's, Arrays (though I think VBA addresses them differently), Subroutines and Procedures etc. (Interestingly what you now call a Subroutine we called a Procedure, Subroutines are (were) something different then!) and no problem understanding numeric, Boolean and string variables. The biggest problem I am having is seeing what runs all the time and what doesn't. I was rather expecting a reference to a cell value would monitor that cell value but not so.
Well you nicely have a head start then, you seem to know what you need to test too which is good. Nothing worse than aimlessly wandering around without a plan. I'm intrigued to know why calling the sub routine may not work but including it in the master sub would, that would mean that some of the variables are outside the 'scope' of the module or something? Are you declaring any/all variables, everything in the module as Public at the top of your code?
spreadbetting
Posts: 3140
Joined: Sun Jan 31, 2010 8:06 pm

firlandsfarm wrote:
Wed Nov 20, 2019 6:28 pm

I've had a look at that exchange on MrExcel and frankly it seems to conclude 'don't use EnableEvents'! He comments "You need to be careful with this if your macros depend on the values of formulas AND your code changes the values of the cells those formulas use." and the questioner concludes "Since my macros often include a need for these things to happen, it's probably best if I not use these two features." … mine likewise (and I would have thought most wb tasks) have values that rely on values so if I turn off calculating by whichever means the value that triggers the bet is not changed! I have turned off automatic updating on large ws in the past but mainly for data input when I haven't wanted an update after every cell entry. What actually happens when the Calculate in turned back on again?

Excel is quite efficient firlands so only recalculates a sheet if it needs to. So once BA sends the data to excel, excel will calculate all your formulas and amend them on the sheet, that will then trigger worksheet_change or calculate as appropriate. If our code is re-writing data back to the excel sheet this could obviously case further formulas on the sheet to change re-triggering another worksheet_calculate or if it writes data back to the sheet another worksheet_change so that would be why we turn off events during the period our code runs, once we turn it back on at the end of our code excel has already done any new calculations as they occur i.e when the code puts then back to the sheet which if coded right would occur before we turn events on again

So

Worksheet_Change - kicked off because a change happens on the sheet
Application.EnableEvents = False - we now disable events so it doesn't get retriggered if we change the sheet with our code

Run our code now

If our code writes to cell A5 which triggers an excel IF statement. Excel would process this now
If our code now writes to cell B5 which triggers another excel IF statement. Excel would process this now it won't store them up

Our code has finished writing to the sheet and excel has finished any calculations as they occur as is, we can now safely set events to true for the next refresh and end our sub

Application.EnableEvents = True
End Sub

Because excel will react to each change on the sheet we can turn off calculations with Application.Calculation = xlCalculationManual this will then stop excel continually recalculating every time we write a cell to the sheet and if we haven't been too clever with our coding that may be many times. Once we turn calculations back on excel will recalculate all formulas in one go so we need to have that put before we enable events again if using worksheet_calculate

Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub

The most efficient way to run your bots is to grab your dataset into an array, do any adjustments to the array in memory , then dump that array back to the sheet. VBA runs much quicker in memory and the data is written back in one lot rather than cells being changed one at a time. Hopefully that makes some sense regarding EnableEvents but like everything the devil is in the details so you need to tailor your code to what you're actually doing. I don't use Worksheet_Calculate or turn calculations off with Application.Calculation because everything is done in VBA so no need for it for me.
jamesg46
Posts: 3769
Joined: Sat Jul 30, 2016 1:05 pm

I've downloaded all of this years GB horseeacing csv files and put them into one excel table, using power query I've been able to separate the columns using the delimetere feature.... the problem I'm now faced with is, I have over 8000 rows of data for the year and for each race there is multiple market I.D duplicates. I want to keep all the records but I want to be able to add total volume for each market so I can then calculate each runners % of the market share.

Can one of you guys give me any advice or at least the function/features I need to research which will allow me to do what I need. (As you can tell, I'm just getting to know Excel)
User avatar
firlandsfarm
Posts: 2722
Joined: Sat May 03, 2014 8:20 am

Hi Callum, Spreadbetting. Sorry I'm not ignoring you just been very busy with my other passion of looking for bargain cars to flip! From nothing crossing my path for weeks I suddenly find 4. Will be back soon.
User avatar
firlandsfarm
Posts: 2722
Joined: Sat May 03, 2014 8:20 am

jamesg46 wrote:
Fri Nov 22, 2019 8:14 pm
I've downloaded all of this years GB horseeacing csv files and put them into one excel table, using power query I've been able to separate the columns using the delimetere feature.... the problem I'm now faced with is, I have over 8000 rows of data for the year and for each race there is multiple market I.D duplicates. I want to keep all the records but I want to be able to add total volume for each market so I can then calculate each runners % of the market share.

Can one of you guys give me any advice or at least the function/features I need to research which will allow me to do what I need. (As you can tell, I'm just getting to know Excel)
Hi James, I notice you didn't get a response to your request. Maybe you now have a way of doing this but my advice would be to use Access. I find Access can be more powerful than Excel for certain tasks with data. For example with Access you can ask it to group data by a field value such as Bf's EventID (for race totals), SelectionID (for the runner's history) etc. And it can easily pull such as Max and Min values etc. Access can import Excel tables, reshape the data as you want and then export it back to Excel for further processing all in one click! :) If you haven't used Access I would urge you to give it a try, I find Excel is very limited with large tables.
User avatar
firlandsfarm
Posts: 2722
Joined: Sat May 03, 2014 8:20 am

CallumPerry wrote:
Wed Nov 20, 2019 7:29 pm
Are you declaring any/all variables, everything in the module as Public at the top of your code?

Hi Callum, just wanted to let you know that I have resolved one of my shortcomings and in the process have managed to have a response and solution from BA … I hope they formalise what's in the thread … viewtopic.php?f=20&t=21033 … because I know I'm not the only one to have problems with this. It's probably the first problem anyone new to VBA on BA will come across. They've even published a revised spreadsheet for download although I think it goes too far, it clears everything including your Bet Placement formula, your Odds formula and any formula/entry for staking! I have added a post to the thread warning users of this and advising how to adapt the code which as you will know is quite simple. Anyway now I have got my macro to run I can now start to concentrate on how to improve it. So …

I'm now going to tackle moving my formulae from the cells and into VBA. I don't need the speed gain but it will be a good exercise to learn on. :) At the moment except for clearing the cells on change of Market everything is in the cells and nothing in VBA.
spreadbetting wrote:
Wed Nov 20, 2019 8:20 pm
Hopefully that makes some sense regarding EnableEvents but like everything the devil is in the details so you need to tailor your code to what you're actually doing.
So Spreadbetting's comments on structuring the code will now come into play. I wanted to get a working macro first (which I now have) and make improvements after.

Again, many thanks to both of you for your assistance.
jamesg46
Posts: 3769
Joined: Sat Jul 30, 2016 1:05 pm

firlandsfarm wrote:
Wed May 20, 2020 7:15 am
jamesg46 wrote:
Fri Nov 22, 2019 8:14 pm
I've downloaded all of this years GB horseeacing csv files and put them into one excel table, using power query I've been able to separate the columns using the delimetere feature.... the problem I'm now faced with is, I have over 8000 rows of data for the year and for each race there is multiple market I.D duplicates. I want to keep all the records but I want to be able to add total volume for each market so I can then calculate each runners % of the market share.

Can one of you guys give me any advice or at least the function/features I need to research which will allow me to do what I need. (As you can tell, I'm just getting to know Excel)
Hi James, I notice you didn't get a response to your request. Maybe you now have a way of doing this but my advice would be to use Access. I find Access can be more powerful than Excel for certain tasks with data. For example with Access you can ask it to group data by a field value such as Bf's EventID (for race totals), SelectionID (for the runner's history) etc. And it can easily pull such as Max and Min values etc. Access can import Excel tables, reshape the data as you want and then export it back to Excel for further processing all in one click! :) If you haven't used Access I would urge you to give it a try, I find Excel is very limited with large tables.
Thank you, I was sick to death of copy & paste, a macro would work but not for any more than 2 times. I'll spend some time with Access, hopefully it makes my life much easier.
jamesg46
Posts: 3769
Joined: Sat Jul 30, 2016 1:05 pm

Finally I worked it out, this was what had caused me so much head ache. Atho was great, really helped me heaps.
You do not have the required permissions to view the files attached to this post.
jamesg46
Posts: 3769
Joined: Sat Jul 30, 2016 1:05 pm

There is a user on the forum and I cant remember his name but he made a very good spreadsheet that collected data from the market pre off or inplay & it had a section where he had done a Pearson correlation, can anyone link me to that sheet, i'm after the Youtube video that goes with it.
User avatar
murdok
Posts: 151
Joined: Sun Apr 02, 2017 7:10 pm

jamesg46 wrote:
Sat May 30, 2020 6:46 pm
There is a user on the forum and I cant remember his name but he made a very good spreadsheet that collected data from the market pre off or inplay & it had a section where he had done a Pearson correlation, can anyone link me to that sheet, i'm after the Youtube video that goes with it.
this one .... viewtopic.php?f=54&t=8896
jamesg46
Posts: 3769
Joined: Sat Jul 30, 2016 1:05 pm

murdok wrote:
Sat May 30, 2020 6:49 pm
jamesg46 wrote:
Sat May 30, 2020 6:46 pm
There is a user on the forum and I cant remember his name but he made a very good spreadsheet that collected data from the market pre off or inplay & it had a section where he had done a Pearson correlation, can anyone link me to that sheet, i'm after the Youtube video that goes with it.
this one .... viewtopic.php?f=54&t=8896
Yes this one, thanks murdok!
Post Reply

Return to “Bet Angel - Spreadsheet / Excel chat”