Function total_rnk(bst, w As Range) As Variant

Dim ss(99), total(99), total_mx(99), Asbaqya(99), qur3a(99), Asbaqya_cof(99), z(99) As Variant, st, fn As Range

Set fn = [a1000].End(xlUp)
Set st = fn.End(xlUp)

play_N = WorksheetFunction.CountA(Range(st, fn))

  For p = 1 To play_N
    For i = 1 To 3
        total(i) = Cells(st.Row, bst.Column).Offset(s - 1, i - 4)
          If WorksheetFunction.IsNumber(total(i)) Then
              total(i + 3) = total(i) + (3 - i) * 0.1
          Else
              total(i + 3) = 0
          End If
        
        If total_mx(s) < total(i + 3) Then total_mx(s) = total(i + 3)
            
          'Asbaqya
        If total(i) > "X" Then
            total(i + 6) = Right(total(i), Len(total(i)) - 1)
        Else: total(i + 6) = 0
        End If
        If Asbaqya(p) < total(i + 6) Then Asbaqya(s) = total(i + 6)
    Next i
  mx_qur3a = WorksheetFunction.Max(Range("A:A"))
  qur3a(s) = (mx_qur3a - Cells(st.Row + s - 1, 1).Value) / mx_qur3a / 100
  
  If mx_Asbaqya < Asbaqya(s) Then mx_Asbaqya = Asbaqya(s)
    
  Next s
  
  'calculate each z(s)
   For s = 1 To play_N
      ws = Cells(st.Row - 1 + s, w.Column).Value
    If mx_Asbaqya = total_mx(s) Then
        Asbaqya_cof(s) = -total_mx(s) / 4 / ws
    Else: Asbaqya_cof(s) = 0
    End If

    z(s) = (clen_mx(s) + (clen_mx(s) / ws)) + 0.00009 + Asbaqya_cof(s) + qur3a(s)
  Next s
  
  'calculate Rank of bst
  s = bst.Row - st.Row + 1
  If z(s) < 1 Then total_rnk = "X": Exit Function
  
  rnk = 1 ' assume
  For s = 1 To play_N
    If z(s) > z(s) Then rnk = rnk + 1
  Next s
      
      
     total_rnk = rnk
    
End Function