What's wrong with this VBA code please?

Please post any questions regarding the program here.

Moderator: 2020vision

What's wrong with this VBA code please?

Postby danjuma » Thu Dec 29, 2011 1:39 pm

Would be very grateful if somebody could please point out what's wrong with this VBA code please?

Code: Select all
If RPdatacopied = False Then
 Sheets("Win").Range("BS5:BT40").Value = ""
 endR = Sheets("RPData").Range("A1").Value
 r2 = 5
For r = 3 To endR
  If Sheets("RPData").Cells(r, 7).Value = 1 Then
     Sheets("Win").Cells(r2, 71).Value = Sheets("RPData").Cells(r, 3).Value
     Sheets("Win").Cells(r2, 72).Value = Sheets("RPData").Cells(r, 6).Value
  End If
  r2 = r2 + 1
Next r
 RPdatacopied = True
End If


Basically, data is meant to be copied from sheet "RPData" to sheet "Win" starting from row 5, anytime I change markets (i.e. the variable 'RPdatacopied' becomes False. Data is being copied alright, but for some reason it is not starting from row 5 in sheet "Win", and just keeps starting from different rows randomly - sometimes row 30, sometimes row 57 and so on. So, the problem would seem to be with the variable r2, but I can't understand as I thought I have made it to start from row 5 by r2 = 5. Help please!

Thanks
Dan
User avatar
danjuma
 
Posts: 347
Joined: Mon Apr 21, 2008 4:17 pm

Postby alrodopial » Thu Dec 29, 2011 8:42 pm

I don't think it solves your problem but should't the r2=r2+1 be inside the previous if statement?
Code: Select all
If Sheets("RPData").Cells(r, 7).Value = 1 Then
     Sheets("Win").Cells(r2, 71).Value = Sheets("RPData").Cells(r, 3).Value
     Sheets("Win").Cells(r2, 72).Value = Sheets("RPData").Cells(r, 6).Value
     r2 = r2 + 1
  End If
 
alrodopial
 
Posts: 1386
Joined: Wed Dec 06, 2006 9:59 pm

Postby danjuma » Thu Dec 29, 2011 9:11 pm

alrodopial wrote:I don't think it solves your problem but should't the r2=r2+1 be inside the previous if statement?
Code: Select all
If Sheets("RPData").Cells(r, 7).Value = 1 Then
     Sheets("Win").Cells(r2, 71).Value = Sheets("RPData").Cells(r, 3).Value
     Sheets("Win").Cells(r2, 72).Value = Sheets("RPData").Cells(r, 6).Value
     r2 = r2 + 1
  End If
 


Thanks for the reply alrodopial. I won't have thought so. Basically, I have 7 columns and any number of rows up to 500 of data in sheet 'RPData'. Column 4 contains the race details (in the format exactly as in sheet 'Win' A1). Column 3 contains horse's names, and column 6 contains the RP odds. When I select a race on BA (which is linked to my workbook), I have an excel formula in column 7 that inserts 1 in the cell against the name of a horse that's in that race e.g. if sheet 'RPData' cell D3 = sheet 'Win' cell $A$1, cell sheet 'RPData'cell F3 = 1. So my code above is to loop through the rows in RPData starting from row 3 to the number of rows with data, and copy the horse's name and odds that have got 1 in column 7 (F) to columns 71 (BS) and 72 (BT) in sheet 'Win'. But to copy this data to sheet 'Win' starting from row 5, and next row until no more selections. Hence reason why I think inserting r2=r2 + 1 where you have suggested won't work. Like I already stated, it is copying the correct data ok, but just not copying to sheet 'Win' starting from row 5 for some reason, and just starting from random rows. Thanks
User avatar
danjuma
 
Posts: 347
Joined: Mon Apr 21, 2008 4:17 pm

Postby alrodopial » Fri Dec 30, 2011 3:19 pm

Sorry, I can't help you but the way I see your code, the purpose of r2 is to find/declare the next free row (where the next data will be copied to) and so it must be increasing only if data has JUST been copied (Sheets("RPData").Cells(r, 7).Value = 1) , that's why I suggested it should be inside the if statement ( data just copied, increase the r2 so next data can be copied at free row)
This is my view
alrodopial
 
Posts: 1386
Joined: Wed Dec 06, 2006 9:59 pm

Postby danjuma » Fri Dec 30, 2011 4:41 pm

alrodopial wrote:Sorry, I can't help you but the way I see your code, the purpose of r2 is to find/declare the next free row (where the next data will be copied to) and so it must be increasing only if data has JUST been copied (Sheets("RPData").Cells(r, 7).Value = 1) , that's why I suggested it should be inside the if statement ( data just copied, increase the r2 so next data can be copied at free row)
This is my view


Thanks for your reply alrodopial, much appreciated. I will try your suggestion tomorrow and see if it solves the issue. Cheers
User avatar
danjuma
 
Posts: 347
Joined: Mon Apr 21, 2008 4:17 pm

Postby danjuma » Sun Jan 01, 2012 5:26 pm

alrodopial wrote:I don't think it solves your problem but should't the r2=r2+1 be inside the previous if statement?
Code: Select all
If Sheets("RPData").Cells(r, 7).Value = 1 Then
     Sheets("Win").Cells(r2, 71).Value = Sheets("RPData").Cells(r, 3).Value
     Sheets("Win").Cells(r2, 72).Value = Sheets("RPData").Cells(r, 6).Value
     r2 = r2 + 1
  End If
 


alrodopial, I eat my words! I followed your suggestion above, and it's solved the problem. Many thanks for the suggestion. Happy new year!
User avatar
danjuma
 
Posts: 347
Joined: Mon Apr 21, 2008 4:17 pm

Postby alrodopial » Sun Jan 01, 2012 10:09 pm

Glad I helped,
that's why we are here, to help each other.
Happy new year to everyone!
alrodopial
 
Posts: 1386
Joined: Wed Dec 06, 2006 9:59 pm


Return to Help

Who is online

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