SWV wrote:Captain Sensible ok thanks. All gets above me this. I've spent hours on this over the weekend. You think you've cracked it but its never that easy with excel is it

Yep there's a lot to take in but once you grasp the basics of how BA and excel work together VBA becomes a lot easier.
Basically when BA refreshes it sends data to excel in two chunks, first one is the set of updated prices (columns A:P) and the next chunk is the betting data and any additional columns (column T onwards).
To fire our VBA code we use what excel calls Events, these events occur when the excel sheet is amended in some way. Because that set of price data changes loads of cell values we usually us Worksheet_Change. The Worksheet_Change event simply sees one or many cells have changed and then runs the VBA code within that sub routine.
- Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
'Our code fires in here
End Sub
Must admit I never use
If Not Intersect, it seems to be used mainly when people are setting up manual spreadsheets for whatever reason. Because BA is sending data on each refresh it never seems neccessary to me and I'd simply use an If condition to trigger any routine. If something is occuring in O5:O6 that's needed i'd just include it in my condition i.e.
If Range("X1") = True And Range("O5") = 1 ThenOnce you have some working code it's then mainly about making it effiecient by not running code when it's uneccessary, so things like turning off events whilst code runs
- Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False 'Turn off events so changes to cell don't retrigger event
'Our code fires in here
Application.EnableEvents = True 'Turn on events again
End Sub
Or even exiting the sub routine early if it's not a price data refresh
- Code: Select all
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Columns.Count <> 16 Then Exit Sub ' Only run on price data refresh
Application.EnableEvents = False 'Turn off events so changes to cell don't retrigger event
'Our code fires in here
Application.EnableEvents = True 'Turn on events again
End Sub