VBA code needs a bit of tweaking

Please post any questions regarding the program here.

Moderator: 2020vision

VBA code needs a bit of tweaking

Postby danjuma » Thu Mar 18, 2010 6:24 pm

Need a bit of help please guys. Got the following VBA code in sheet 1. which works fine for that workbook. However, when I have two or more duplicate workbooks opened, the code does not work as it should (affected by the duplicate workbooks). The way I have got around this is to open each duplicate workbook in separate instances of excel. How can I get the code to be workbook specific, so that I would be able to open duplicate workbooks in the one instance of excel.

Many thanks
Dan

Dim refreshCount As Integer

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Columns.Count <> 16 Then Exit Sub
Application.EnableEvents = False
If Range("y1").Value = 1 Then
refreshCount = refreshCount + 1
Else
refreshCount = 0
End If
If refreshCount > 5 Then
Range("y2").Value = 1
Else
Range("y2").Value = 0
End If
If Cells(2, 5) = "In Play" Then
If Cells(1, 27) = "" Then Cells(1, 27) = Cells(2, 3)
If Cells(1, 27) <> "" Then Cells(1, 28) = DateDiff("s", Cells(1, 27), Cells(2, 3))
End If
If Cells(2, 5) <> "In Play" And Cells(2, 6) <> "Suspended" Then
Cells(1, 27) = ""
Cells(1, 28) = ""
End If
Application.EnableEvents = True
End Sub


PS. Not that versed in VBA, the above code was just a mix & match of other people's codes.
User avatar
danjuma
 
Posts: 347
Joined: Mon Apr 21, 2008 4:17 pm

Postby osknows » Thu Mar 18, 2010 7:58 pm

One way is to replace all references to Range and Cell objects with the following

ThisWorkbook.Sheets(Target.Worksheet.Name).Range("y1").Value

and

ThisWorkbook.Sheets(Target.Worksheet.Name).Cells(2, 5)


'ThisWorkbook' assumes the workbook the code is running in and 'Target.Worksheet.Name' picks up the name of the worksheet the event was fired from
User avatar
osknows
 
Posts: 946
Joined: Wed Jul 29, 2009 12:01 am

Postby danjuma » Thu Mar 18, 2010 8:25 pm

Thanks osknows, will try your suggestion. Cheers :D
User avatar
danjuma
 
Posts: 347
Joined: Mon Apr 21, 2008 4:17 pm

Postby danjuma » Fri Mar 19, 2010 6:29 pm

Works fine osknows. Many thanks! :D
User avatar
danjuma
 
Posts: 347
Joined: Mon Apr 21, 2008 4:17 pm

Postby mak » Sun Mar 28, 2010 8:56 am

osknows wrote:One way is to replace all references to Range and Cell objects with the following

ThisWorkbook.Sheets(Target.Worksheet.Name).Range("y1").Value

and

ThisWorkbook.Sheets(Target.Worksheet.Name).Cells(2, 5)


'ThisWorkbook' assumes the workbook the code is running in and 'Target.Worksheet.Name' picks up the name of the worksheet the event was fired from


Can someone clarify, if i have to replace "ThisWorkbook" with the actual name of workbook or to keep it as it is (the same for worksheet?) :oops:
mak
 
Posts: 1086
Joined: Tue Jun 30, 2009 8:17 am

Postby alrodopial » Sun Mar 28, 2010 10:42 am

mak wrote:
Can someone clarify, if i have to replace "ThisWorkbook" with the actual name of workbook or to keep it as it is (the same for worksheet?) :oops:


You don't have to replace it.
alrodopial
 
Posts: 1355
Joined: Wed Dec 06, 2006 9:59 pm

Postby mak » Sun Mar 28, 2010 11:48 am

alrodopial wrote:
mak wrote:
Can someone clarify, if i have to replace "ThisWorkbook" with the actual name of workbook or to keep it as it is (the same for worksheet?) :oops:


You don't have to replace it.


thanks al :)
mak
 
Posts: 1086
Joined: Tue Jun 30, 2009 8:17 am


Return to Help

Who is online

Users browsing this forum: Google [Bot] and 107 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.