Worksheet_Change code question

Please post any questions regarding the program here.

Moderator: 2020vision

Worksheet_Change code question

Postby Smokin Joe » Sat Jun 07, 2008 11:28 am

I have successfully written a bit of code which clears columns T:W if Col T reads "CANCELLED".

The code read as follows:
' Check for "CANCELLED" in Row T
For a = 5 To 150
If ws1.Range("T" & a).Value = "CANCELLED" Then
' Clear bet references
ws1.Range("T" & a & ":W" & a).ClearContents
End If
Next a

I now want to add an extra condition by saying that if the above is true OR if Col AE reads "F" (this looks up to see if the bet has been fully matched) then clear Cols T:W.

The code reads as follows:
' Check for "CANCELLED" in Row T OR "F" in Row AE
For a = 5 To 150
If ws1.Range("T" & a).Value = "CANCELLED" Or ws1.Range("AE" & a).Value = "F" Then
' Clear bet references
ws1.Range("T" & a & ":W" & a).ClearContents
End If
Next a

However, that code gives me a Run-time Error 13: Type Mismatch.

I have ran the code in another spreadsheet unrelated to BA and it works exactly as planned.

Any ideas as to why does it not work within the above code, is it something to do with a Worksheet Change event?
Smokin Joe
 
Posts: 115
Joined: Sat May 31, 2008 6:25 am

Postby KevinTHFC » Sat Jun 07, 2008 4:44 pm

I am guessing that the "F" in AE is actually a boolean rather than a string and that is why you are getting the type mismatch error.
KevinTHFC
 
Posts: 72
Joined: Fri Aug 25, 2006 9:08 pm

Postby Smokin Joe » Sat Jun 07, 2008 5:36 pm

KevinTHFC wrote:I am guessing that the "F" in AE is actually a boolean rather than a string and that is why you are getting the type mismatch error.


The "F" is generated from a lookup.

Kevin, how do I correct my code then?

Cheers
Smokin Joe
 
Posts: 115
Joined: Sat May 31, 2008 6:25 am

Postby KevinTHFC » Sun Jun 08, 2008 10:53 am

I assume that the error is coming on the
Code: Select all
If ws1.Range("T" & a).Value = "CANCELLED" Or ws1.Range("AE" & a).Value = "F" Then


If so what are the values of the two elements of the IF statement at the time of the error.

Kev
KevinTHFC
 
Posts: 72
Joined: Fri Aug 25, 2006 9:08 pm

Postby Smokin Joe » Sun Jun 08, 2008 11:15 am

KevinTHFC wrote:I assume that the error is coming on the
Code: Select all
If ws1.Range("T" & a).Value = "CANCELLED" Or ws1.Range("AE" & a).Value = "F" Then


If so what are the values of the two elements of the IF statement at the time of the error.

Kev


Kev, yes that is correct. The error is happening at that code.

How do I know the values at the time of breaking? Can I type something into the immediate pane to determine the values (as I have now way of knowing what row it stopped at)?

Cheers
Smokin Joe
 
Posts: 115
Joined: Sat May 31, 2008 6:25 am

Postby KevinTHFC » Sun Jun 08, 2008 11:31 am

If you type a ? followed by the variable it will display the value.

For example if you typed
Code: Select all
?Activecell.Row

The current row number will be displayed

if you typed
Code: Select all
?a

The value of a will be displayed

So you should be able to enter
Code: Select all
?ws1.Range("AE" & a).Value

to get the value that is causing the problem.

Kev
KevinTHFC
 
Posts: 72
Joined: Fri Aug 25, 2006 9:08 pm

Postby Smokin Joe » Sun Jun 08, 2008 12:29 pm

I had a rough idea how the immediate pane worked, so thanks for the clarifcation.

I ran the code, and it stopped at the same line.

I then typed:
?ws1.Range("AE" & a).Value in the immediate pane

But I got an error message "Run-time error 424". Object required :oops:

I am none the wiser.
Smokin Joe
 
Posts: 115
Joined: Sat May 31, 2008 6:25 am

Postby Smokin Joe » Sun Jun 08, 2008 2:34 pm

On further testing, the problem seems to be that the F is generated by a lookup formula, ie AE6 is not equal to "F" directly, but rather as a result of the lookup formula which is:
=VLOOKUP(T6,MyBets!A$2:E$100,5,FALSE)

So, is it possible to use the result of a lookup formula in the fashion that I am attempting to do it.

Or alternately is there a different way of determing whether my BACK-F5 bet has been matched in full or not, as that is what I am trying to do with this?

I can't use the obvious solution of seeing if my liability = my stake, as the stake is calculated by a Kelly formula and so it changes all the time, so even if my bet was matched in full the liability almost certainly won't equal the newly calculated stake.
Smokin Joe
 
Posts: 115
Joined: Sat May 31, 2008 6:25 am

Postby Smokin Joe » Sun Jun 08, 2008 3:41 pm

At the risk of talking to myself, I have now (categorically) discovered what the problem is, and I was talking rubbish in the previous post :oops: .

Col AE has lookups and some cells have #N/A as the solution (ie the lookup isn't matched). This is then what causes the error in my code.

The code doesn't know what to do if ws1.Range("AE" & a).Value = #N/A

So I'm going to have to thing of an alternative solution, as the lookup doesn't have the desired effect.
Smokin Joe
 
Posts: 115
Joined: Sat May 31, 2008 6:25 am

Postby KevinTHFC » Sun Jun 08, 2008 7:16 pm

Hi SJ,

Been out all afternoon. Yep the N/A will cause the problem

Kev
KevinTHFC
 
Posts: 72
Joined: Fri Aug 25, 2006 9:08 pm


Return to Help

Who is online

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