Results 1 to 10 of 20

Thread: How to calculate best bowling figure (cricket)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    _...... continued from last post https://excelfox.com/forum/showthrea...ll=1#post15578

    In most practical situation that last event coding would likely cause problems as it will be running many times unnecessarily: It runs every time you change anything anywhere in Sheet 3 ( This is not the problem discussed at the end of the last post: It's a seperate issue )

    We would generally prefer to limit the coding to when we change something in a restricted area. We can achieve that in many ways.
    Most conveniently, Excel gives us a way to know in the coding which range was changed: Just before the Event macro runs, the range object of the range changed is assigned to that variable Target in the first code line, Private Sub Worksheet_Change(ByVal Target As Range)
    Often towards the start of the macro we would have some If thing to see if the area of interest was changed. I have done that in the uploaded file, Stats_Template Event_1.xls

    In the macro I have done for you, the important line is this one:
    Code:
       If Application.Intersect(Target, DtaRng) Is Nothing Then ' ------------------------------------------------------------
    To explain
    I have arranged that DtaRng is the range of column D and E with your run and wicket data in it.
    Target , as I explained, is assigned by Excel already to be the range that was changed.
    The Intersect function is used to return a range object equal to that where any supplied ranges ( in our case DtaRng and Target) coincide ( where they overlap or intersect ). If, however the ranges don’t overlap, then the Intersect function returns us Nothing. The range you change , Target , and the data range, DtaRng , will not overlap ( intersect ) if you did not change a cell in the data range.
    So I am testing to see if it returns nothing.
    __ If it returns Nothing, Then I Do Nothing
    __ Otherwise ( Else ) the coding is done.

    Here the full macro:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)   ' Target will be filled just before this macro starts , with the range object of the cell or cells that were changed
    Rem 0 In this section are some things and ideas that might be useful if we extend this coding to an efficient macro for all sections.
    Dim N As Long ' For this macro this is a single value.  It might be a loop bound index variable in a further macro.
     Let N = 1
    Dim StrtRw As Long: Let StrtRw = ((N - 1) * 23) + 1 '
    Dim Rws As Long: Let Rws = 21
    Dim DtaRng As Range: Set DtaRng = Range("D" & StrtRw + 1 & ":E" & StrtRw + (Rws - 1) & "") ' this will be the data range  in columns D and E in a section
        
       If Application.Intersect(Target, DtaRng) Is Nothing Then ' ------------------------------------------------------------
       ' Do Nothing
       Else ' We will come here if we changed something in DtaRng, because there was a overlap / (intersect) in Target and the data range, DtaRng
        Rem 1 Worksheets info
        Dim Ws3 As Worksheet: Set Ws3 = ThisWorkbook.Worksheets("Sheet3")
        Dim Mtchs As Long: Let Mtchs = 20
        Dim FstMtch As Long: Let FstMtch = 2
        Dim MxE As Long
        Rem 2
        '2a   =MAX(E2:E21)
        ' Let MxE = Evaluate("=MAX(E2:E21)") ' This basic formula will work, if you are lucky, but probably better to do it like in the next line
         Let MxE = Ws3.Evaluate("=MAX(E" & FstMtch & ":E" & FstMtch + (Mtchs - 1) & ")")
        '2b
        Dim MnD As Long: Let MnD = 9999
        Dim Cnt As Long
            For Cnt = FstMtch To FstMtch + (Mtchs - 1) Step 1
             If Ws3.Range("E" & Cnt & "").Value = MxE And Ws3.Range("D" & Cnt & "").Value < MnD Then Let MnD = Ws3.Range("D" & Cnt & "").Value
            Next Cnt
        Rem 3 Output
        ' Let Ws3.Range("M22").Value = MnD & "_" & MxE
         Let Application.EnableEvents = False
         Let Ws3.Range("M" & FstMtch + Mtchs & "").Value = MnD & "_" & MxE '  ... using variables rather than hard coding 22, as I am thinking ahead a bit for us, for a final macro which does all sections
         Let Application.EnableEvents = True
        End If ' -------------------------------------------------------------------------------------------------------------
    
    End Sub
    




    I am happy to help further and / or explain anything further, as long as you are not in a hurry, as I am only sporadically near the forum just now
    ( By the way, if you are new to forums, you may not know that there are some other more busy forums such as excelforum.com and mrexcel.com. These are often better if you need a quick answer as there are lots of people there all the time eager to give a quick solution. You may not always get such detailed explanation. If you have the time and want to learn more, then probably here at excelfox.com is better, but just now there are less people here, so you probably won’t get a quick response. )

    Alan
    Attached Files Attached Files
    Last edited by DocAElstein; 08-13-2021 at 08:18 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

Similar Threads

  1. Replies: 7
    Last Post: 06-17-2019, 04:34 PM
  2. Replies: 1
    Last Post: 04-24-2013, 04:34 PM
  3. Calculate the Hours
    By leopaulc in forum Excel Help
    Replies: 2
    Last Post: 02-27-2013, 02:51 PM
  4. Runtime Error 481 invalid figure when PNG
    By Tony in forum Excel Help
    Replies: 0
    Last Post: 02-12-2013, 12:59 AM
  5. Add Overs in Cricket
    By MrDelhi in forum Excel Help
    Replies: 1
    Last Post: 07-15-2011, 09:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •