Getting a macro to run continuously

Please post any questions regarding the program here.

Moderator: 2020vision

Getting a macro to run continuously

Postby Ferru123 » Sun Jul 16, 2017 11:44 am

Hi

I'd like to get a macro to run every time the spreadsheet is updated with new Betfair data.

I've read online that you can do this by using Private Sub Worksheet_Change(ByVal Target As Range). However, my macro below only works when I manually select 'Run' in Excel.

Any assistance would be much appreciated. :)

Option Explicit

Dim currentMarket As String, marketSelected As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

End Sub

Sub Macro1()

If Range("AC5").Value = 1 Then

Range("AC5").Copy

Range("AD5").PasteSpecial Paste:=xlPasteValues

End If

End Sub


Thanks

Jeff
Ferru123
 
Posts: 87
Joined: Sat Feb 21, 2009 1:28 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Sun Jul 16, 2017 2:34 pm

Is that your ful code Jeff? If so it's not running because the maco you're using isn't within the worksheet_change routine

Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

End Sub


You'd just need to amend it to something like

Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

Call Macro1 

End Sub



Or simply replace your macro with a line like this to do the sae thing

Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

If Range("AC5").Value = 1 Then Range("AD5").Value =Range("AC5").Value

End Sub
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Ferru123 » Sun Jul 16, 2017 4:54 pm

Thanks, much appreciated. :)

Jeff (VBA complete newbie, in case there was any doubt!)
Ferru123
 
Posts: 87
Joined: Sat Feb 21, 2009 1:28 pm

Re: Getting a macro to run continuously

Postby Ferru123 » Sun Jul 16, 2017 6:00 pm

I've amended my code to the following:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

Call Macro1

End Sub

Sub Macro1()

If Range("AC5").Value = 1 Then

Range("AC5").Copy

Range("AD5").PasteSpecial Paste:=xlPasteValues

End If

End Sub


If I run the macro manually and have a 1 in AC5, it's copied to AD5.

However, if I delete both cells and put 1 into AC5, nothing happens.

What do you suggest?

Thanks

Jeff
Ferru123
 
Posts: 87
Joined: Sat Feb 21, 2009 1:28 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Sun Jul 16, 2017 6:22 pm

Is the sheet linked to excel so it's continually refreshing the 16 columns in your code If Target.Columns.Count <> 16 Then Exit Sub? Otherwise it'll just exit the worksheet change routine because 16 columns haven't changed (16 columns is a full price data refresh)
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Sun Jul 16, 2017 6:24 pm

Meant linked to Betting assistant
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Ferru123 » Sun Jul 16, 2017 9:44 pm

Hi

Yes, it was.

I was going to attached the spreadsheet, but I'm not able to attach xlsm extension spreadsheets.

Thanks

Jeff

Captain Sensible wrote:Meant linked to Betting assistant
Ferru123
 
Posts: 87
Joined: Sat Feb 21, 2009 1:28 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Sun Jul 16, 2017 10:04 pm

Hard to know why it's not working if it was getting it's refreshes from Betting Assistant. Maybe excel needed debugging and had turned events off, hard to know without being there tbh. Probably not a bad thing having things crash on you whilst testing though as it forces you learn good practices early on and also how to get things running again. Try going into the the VBA project windows and look at the Debug and Run menus on the top line see if things need to be reset/restarted. I think the code you're using to copy isn't the best coding and may have caused a stack overflow. It's much easier just to use simple code like this if you want to copy cell contents to another cell rather than doing all the copy and paste as it's more efficient.


Code: Select all
If Range("AC5").Value = 1 Then Range("AD5").Value =Range("AC5").Value
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Ferru123 » Mon Jul 17, 2017 10:22 am

Thanks Captain Sensible.

I've amended the code to the following, but am still having the same issue:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub

Call Macro1

End Sub

Sub Macro1()

If Range("AC5").Value = 1 Then Range("AD5").Value = Range("AC5").Value

End Sub


I'm using Excel 2007. Do you think upgrading to a more recent version of Excel might solve the problem?

Thanks

Jeff
Ferru123
 
Posts: 87
Joined: Sat Feb 21, 2009 1:28 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Mon Jul 17, 2017 1:37 pm

It's a simple sub routine so should work fine, maybe best to just start from the basics. You can also echo things out to the screen i.e. a cell just to make sure the events are actually firing, something as simple as a counter in say AA1 would show you at a glance if your connection to Betting Assistant is working OK and then just delete that code when everything's OK.

So if we go from the very start assuming you know nothing.

So first off we'll make the developer tab visible on excel if it's not already been done, just so you can navigate there easily. Go to file menu at the top , select Options->Customise ribbon->tick developer->OK

Image

You should now have the developer tab at the top of the excel screen, click that and then click the Visual Basic icon which takes you to the coding screens. Now we want to add a module where we can store all separate sub routines like your macro1. Go to the Insert menu at the top and click module, you should see a new folder added to the left hand side of the screen under the VBA project called Modules, in that folder will be module1 where we can store sub routines, we can add other modules as we wish so you can group similar sub routines together to make things easily to edit later or even export to other excel sheets.

Your VBA screen should look a little like this

Untitled.png
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Mon Jul 17, 2017 2:57 pm

If you've managed the above we can now set up some simple Worksheet_Change code.

In simple terms excel looks for events occuring and one of those events is a change in a cell on the sheet, if excel detects a change our worksheet_change routine kicks in and runs any code within it.

When Betting Assistant is linked to excel it sends 2/3 sets of data on each refresh, one set contains the market data like race time etc another contains the price data and the last additional columns. Obviously the price data is the one we want to concentrate on any changes so that's why the line

Code: Select all
If Target.Columns.Count <> 16 Then Exit Sub


is included in most of the coding you see on the site, basically if any changes on the sheet affect less than 16 columns (A:P) we exit our worksheet_change sub routine early as there's no point it running thru all the coding. So when you changed AC5 manually it only affected 1 column and the routine exited before it called your copying routine.

Another couple of pieces of code you'll see in examples are

Code: Select all
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'code is in here

Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic



The reason we add these to our code is to stop Events within excel continually triggering our Worksheet_Change routine which is simply looking for the 'event' of a change in a cell, i.e. if our code is writing data to cell AD5 that would trigger another worksheet_change routine. ou could say but we've already accounted for that by sticking in our code If Target.Columns.Count <> 16 Then Exit Sub , the problem there is that although the routine would exit it would still run, if we inadvertaintly had our code changing 16 columns it'd get in a continuous loop. It's much better we simply turn off all events from being able to re-trigger whilst our routine is running. We also have the option of turning off screen updates whilst our code is running which can be useful if your sheet has hundreds of formulas because those continually being written to the screen can slow things down. As we're just using VBA there shouldn't be any need to turn screenupdating off and on.

So now our bare bones Worksheet_change routine would look like this, we've added Option Explicit to the top as that requires us to declare all variables which is simply just good practice, maybe not needed now but may be at a later date. Another thing you'll notice is the use of comments by using an apostrophe ' , VBA ignore the text after the apostrophe. It's good practice to comment your code so you can remember what it's doing, it's very easy to get confused once your code gets biggers and the comments will help you track down parts that need changing etc

So here's your basic code to insert into the Worksheet

Code: Select all
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub 'If columns changed <> 16 then exit sub
Application.EnableEvents = False 'turns off events so we don't retrigger the code
Application.Calculation = xlCalculationManual 'turns off all calculations



'code is in here
Call CopyAC5  ' just your simple copying sub routine



Application.EnableEvents = True ' events back on
Application.Calculation = xlCalculationAutomatic ' calculations back on
End Sub



We now post this code under Sheet1 by double clicking on Sheet1 in the Microsoft Excel Objects on your VBA project screen. We could post it under any of the sheets listed but it needs to go under the excel sheet that is linked to Betting Assistant for it to fire. We can also include code under the tab for ThisWorkbook then it would fire under every sheet in the workbook but for what we're doing it's only eeded under the sheet you will link Betting Assistant to.

Now post the Sub routine into our module1

Code: Select all
Sub CopyAC5()

If Range("AC5").Value = 1 Then Range("AD5").Value = Range("AC5").Value

End Sub


We will also add another simple sub to modules one to reset events if we encounter errors it means we can easily reset them just by firing the macro

Code: Select all
Sub reset()
  Application.EnableEvents = true

End Sub



Now close and save that file as test.xlsm, make sure you save it as a macro enabled file.

Fire up betting assistant and go to the Excel Menu, browse to your test.xslm file and press ok to link excel to betting assistant. Your code should now be active. But be aware your code isn't very useful and will only fire when those 16 columns get sent to excel from BA, depending on how quick you're refreshing from Gruss it may appear to update when you put a 1 in AC5 but the reality is it is only firing when it sends a price refresh to trigger the code because 16 columns have changed. Obviously if changing AC5 is part of some bot then fine but be aware of how and why it is triggering.
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Ferru123 » Mon Jul 17, 2017 10:18 pm

Hello again. :)

Thank you for taking the time to provide such a detailed explanation.

Unfortunately, I still can't get the macro to work continuously. I've attached a screenshot, in case that helps.

VB 1.JPG
VB 1.JPG (91.95 KiB) Viewed 35444 times


I've also tried inserting Call CopyAC5 into the first macro, but that doesn't make any difference.

Any suggestions?

Thanks

Jeff
Ferru123
 
Posts: 87
Joined: Sat Feb 21, 2009 1:28 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Mon Jul 17, 2017 10:43 pm

You need to copy all the code I posted, in your code your lines

Application.EnableEvents = False 'turns off events so we don't retrigger the code
Application.Calculation = xlCalculationManual 'turns off all calculations

Just turn the application events off but never turn them back on again so effectively you shut down the worksheet_change from working.

Delete everything that is currently in the worksheet then copy the all code below and paste it into sheet1

Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Columns.Count <> 16 Then Exit Sub 'If columns changed <> 16 then exit sub
Application.EnableEvents = False 'turns off events so we don't retrigger the code
Application.Calculation = xlCalculationManual 'turns off all calculations



'code is in here
Call CopyAC5 ' just your simple copying sub routine



Application.EnableEvents = True ' events back on
Application.Calculation = xlCalculationAutomatic ' calculations back on
End Sub


Now copy the code below and paste it into Module1

Sub CopyAC5()

If Range("AC5").Value = 1 Then Range("AD5").Value = Range("AC5").Value

End Sub

Sub reset()
Application.EnableEvents = true

End Sub


You may have to run the macro reset to turn your events back on for things to work depending if you've shutdown excel.
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Captain Sensible » Mon Jul 17, 2017 10:46 pm

Make sure you copy the sub codes into the module1 not under sheet1
User avatar
Captain Sensible
 
Posts: 2883
Joined: Sat Nov 19, 2005 2:29 pm

Re: Getting a macro to run continuously

Postby Ferru123 » Tue Jul 18, 2017 10:11 am

Captain Sensible wrote:Make sure you copy the sub codes into the module1 not under sheet1


Thanks.

I put everything into module1.

I'll revisit the spreadsheet later today or tomorrow and let you know how I get on.

Jeff
Ferru123
 
Posts: 87
Joined: Sat Feb 21, 2009 1:28 pm

Next

Return to Help

Who is online

Users browsing this forum: No registered users and 105 guests

Sports betting software from Gruss Software


The strength of Gruss Software is that it’s been designed by one of you, a frustrated sports punter, and then developed by listening to dozens of like-minded enthusiasts.

Gruss is owned and run by brothers Gary and Mark Russell. Gary discovered Betfair in 2004 and soon realised that using bespoke software to place bets was much more efficient than merely placing them through the website.

Gary built his own software and then enhanced its features after trialling it through other Betfair users and reacting to their improvement ideas, something that still happens today.

He started making a small monthly charge so he could work on it full-time and then recruited Mark to help develop the products and Gruss Software was born.

We think it’s the best of its kind and so do a lot of our customers. But you can never stand still in this game and we’ll continue to improve the software if any more great ideas emerge.

cron