Code help please

Please post any questions regarding the program here.

Moderator: 2020vision

Code help please

Postby knot » Sun Dec 07, 2008 9:52 pm

I am using the following formula to place -3 in cell Q2 to refresh the quick pick list at 11.30am, =IF(AND(X2>0.46875,X2<0.469444),-3,2). Cell X2 is the time in decimal format. The formula works well but when -3 is placed in Q2 the formula is deleted. Can i get around this using vba? Im hopless with vba/code ect, any help please? :)
knot
 
Posts: 196
Joined: Sun Feb 04, 2007 9:51 pm
Location: leicester

Postby throwmeadisc » Mon Dec 08, 2008 12:58 pm

Hey knot,

Just knocked up this code for you. Should hopefully do the job.

Notes:
It assumes that your connected Excel Sheet is 'Sheet1' and also uses 'AA1' to record the scheduled time that the refresh will happen. If you try and close the Workbook but then cancel, the scheduled refresh will not happen. To re-enable it - Run the Macro: SetupPicklistRefresh.

Code: Select all
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim RunTime As Date
RunTime = ThisWorkbook.Sheets("Sheet1").Range("AA1").Value
On Error Resume Next
Application.OnTime RunTime, "RefreshPickList", , False
ThisWorkbook.Sheets("Sheet1").Range("AA1").Value = ""
End Sub

Private Sub Workbook_Open()
Call SetupPicklistRefresh
End Sub

Sub RefreshPickList()
Dim RunTime As Date

ThisWorkbook.Sheets("Sheet1").Range("Q2").Value = -3
RunTime = Date + 1 + TimeValue("11:30")
Application.OnTime RunTime, "RefreshPickList", , True
ThisWorkbook.Sheets("Sheet1").Range("AA1").Value = RunTime
End Sub

Sub SetupPicklistRefresh()
Dim RunTime As Date
   
With ThisWorkbook.Sheets("Sheet1").Range("AA1")
    If .Value <> "" Then Application.OnTime .Value, "RefreshPickList", , False
End With
   
RunTime = Date + TimeValue("11:30")
If Now > RunTime Then
    RunTime = Date + 1 + TimeValue("11:30")
    ThisWorkbook.Sheets("Sheet1").Range("AA1").Value = RunTime
End If
Application.OnTime RunTime, "RefreshPickList", , True
End Sub


In the VBA editor put this under "ThisWorkBook"

Cheers,

Al :D
User avatar
throwmeadisc
 
Posts: 165
Joined: Fri Dec 14, 2007 11:06 am
Location: London, UK

Postby knot » Mon Dec 08, 2008 6:24 pm

thanks for the time and effort throwmeadisc :) didnt realize there would would be so much to it :shock: ill give it a go, cheers.
knot
 
Posts: 196
Joined: Sun Feb 04, 2007 9:51 pm
Location: leicester

Postby throwmeadisc » Tue Dec 09, 2008 11:50 am

No worries mate. Suppose it could have been simpler, but I like the Application.Ontime Method as it saves a few CPU clock cycles as you don't havre to keep checking if it's the right time to run something or not.

But with it you need to handle it correctly as sometimes if you don't disable the scheduled item when the workbook is closed, it will re-open it and run the procedure!!

http://www.ozgrid.com/Excel/run-macro-on-time.htm

Anyway, hope it works for you.

Al :)
User avatar
throwmeadisc
 
Posts: 165
Joined: Fri Dec 14, 2007 11:06 am
Location: London, UK

Postby throwmeadisc » Tue Dec 09, 2008 11:59 am

Opps...

Just spotted a slight mistake...

New Code:

Code: Select all
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim RunTime As Date
RunTime = ThisWorkbook.Sheets("Sheet1").Range("AA1").Value
On Error Resume Next
Application.OnTime RunTime, "RefreshPickList", , False
ThisWorkbook.Sheets("Sheet1").Range("AA1").Value = ""
End Sub

Private Sub Workbook_Open()
Call SetupPicklistRefresh
End Sub

Sub RefreshPickList()
Dim RunTime As Date

ThisWorkbook.Sheets("Sheet1").Range("Q2").Value = -3
RunTime = Date + 1 + TimeValue("11:30")
Application.OnTime RunTime, "RefreshPickList", , True
ThisWorkbook.Sheets("Sheet1").Range("AA1").Value = RunTime
End Sub

Sub SetupPicklistRefresh()
Dim RunTime As Date
   
With ThisWorkbook.Sheets("Sheet1").Range("AA1")
    If .Value <> "" Then Application.OnTime .Value, "RefreshPickList", , False
End With
   
RunTime = Date + TimeValue("11:30")
If Now > RunTime Then RunTime = Date + 1 + TimeValue("11:30")

ThisWorkbook.Sheets("Sheet1").Range("AA1").Value = RunTime
Application.OnTime RunTime, "RefreshPickList", , True
End Sub


Changes were made in the Sub SetupPicklistRefresh() section at the bottom.

Sorry mate......

Al 8)
User avatar
throwmeadisc
 
Posts: 165
Joined: Fri Dec 14, 2007 11:06 am
Location: London, UK

Postby Steve Voltage » Sun Jan 11, 2009 1:52 pm

ive modified this code to work on a sheet called 19 at the time of 12:50:00 butnothing happens :( any ideas please.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim RunTime As Date
RunTime = ThisWorkbook.Sheets("19").Range("AA1").Value
On Error Resume Next
Application.OnTime RunTime, "RefreshPickList", , False
ThisWorkbook.Sheets("19").Range("AA1").Value = ""
End Sub

Private Sub Workbook_Open()
Call SetupPicklistRefresh
End Sub

Sub RefreshPickList()
Dim RunTime As Date

ThisWorkbook.Sheets("19").Range("Q2").Value = -3
RunTime = Date + 1 + TimeValue("12:50:00")
Application.OnTime RunTime, "RefreshPickList", , True
ThisWorkbook.Sheets("19").Range("AA1").Value = RunTime
End Sub

Sub SetupPicklistRefresh()
Dim RunTime As Date

With ThisWorkbook.Sheets("19").Range("AA1")
If .Value <> "" Then Application.OnTime .Value, "RefreshPickList", , False
End With

RunTime = Date + TimeValue("12:50:00")
If Now > RunTime Then RunTime = Date + 1 + TimeValue("12:50:00")

ThisWorkbook.Sheets("19").Range("AA1").Value = RunTime
Application.OnTime RunTime, "RefreshPickList", , True
End Sub
Steve Voltage
 

Postby jokerjoe » Sun Jan 11, 2009 6:15 pm

This is based my approach, the if clause is to make sure it doesn't run unnecessarily if you've already refreshed during the day.

Code: Select all
Private Sub Workbook_Open()
    Call Tempo_Market
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime earliesttime:=vartimer_market, procedure:="Tempo_Market", schedule:=False
End Sub


Public vartimer_market As Date

Sub Tempo_Market()
    vartimer_market = Date + 1 + TimeSerial(0, 12, 50)
    If Date > wsSM.Range("B3").Value Then
        wsBA.Range("Q2") = -3
        wsSM.Range("B3").Value = Date + Time
        Application.OnTime TimeValue(vartimer_market), "Tempo_Market"
    Else
        Application.OnTime TimeValue(vartimer_market), "Tempo_Market"
    End If
End Sub
User avatar
jokerjoe
 
Posts: 122
Joined: Wed May 09, 2007 12:00 pm

Postby throwmeadisc » Mon Jan 12, 2009 12:53 pm

Hi,

Are you sure you did this bit correctly....

In the VBA editor put this under "ThisWorkBook"


Al :wink:
User avatar
throwmeadisc
 
Posts: 165
Joined: Fri Dec 14, 2007 11:06 am
Location: London, UK


Return to Help

Who is online

Users browsing this forum: No registered users and 26 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.