Code amendment needed - Load a range of data into memory, perform some calculations and rank then show alert with message box - VBA

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
This post is an upgrade of the problem and solution provided by @DanteAmor post #39 at:

Code:
Sub test()
  Dim lr As Long, a As Variant, b As Variant, c As Variant, sufix As String
  Dim i As Long, j As Long, k As Long, nSum As Double, nRank As Long
  Dim nID As Variant, nMax As Double, sCat As String, f As Range
  Dim nTot As Double, nCon As Long
  
  nID = Val(UserForm1.txtID.Value)

  Set f = Sheet1.Range("B:B").Find(nID, , xlValues, xlWhole)
  If Not f Is Nothing Then
    sCat = f.Offset(, 2)
  End If
  
  lr = Sheet2.Range("B" & Rows.Count).End(3).Row
  a = Sheet1.Range("B7:AA" & lr).Value2
  ReDim b(1 To UBound(a, 1), 1 To 11)
  ReDim c(1 To 1, 1 To 10)
  
  For i = 1 To UBound(a)
    nSum = 0
    For j = 7 To 16
      Select Case a(i, 3)
        Case "X", "Y", "Z"
          b(i, j - 6) = a(i, j) + a(i, j + 10) * 0.2
        Case Else
          b(i, j - 6) = a(i, j) + a(i, j + 10) * 0.1
      End Select
      nSum = nSum + b(i, j - 6)
    Next
    b(i, 11) = nSum
    If a(i, 1) = nID Then
      For k = 1 To 10
        c(1, k) = b(i, k)
      Next
      nMax = nSum
    End If
  Next
  
'RANKS
  nRank = 1
  For i = 1 To UBound(b)
    If a(i, 3) = sCat And b(i, 11) > nMax Then
      nRank = nRank + 1
    End If
  Next
  
  Select Case nRank
    Case 11 To 13
      sufix = "th"
    Case Else
      Select Case Right(nRank, 1)
        Case "1": sufix = "st"
        Case "2": sufix = "nd"
        Case "3": sufix = "rd"
        Case Else: sufix = "th"
      End Select
  End Select
  UserForm1.txtRank.Value = nRank & " " & sufix
  
  For j = 1 To 10
    nRank = 1
    For i = 1 To UBound(b)
      If a(i, 3) = sCat And b(i, j) > c(1, j) Then
        nRank = nRank + 1
      End If
    Next
    Select Case nRank
      Case 11 To 13
        sufix = "th"
      Case Else
        Select Case Right(nRank, 1)
          Case "1": sufix = "st"
          Case "2": sufix = "nd"
          Case "3": sufix = "rd"
          Case Else: sufix = "th"
        End Select
    End Select
    UserForm1.Controls("tb" & j).Value = nRank & " " & sufix
  Next

'AVERAGE
  For j = 1 To 11
    nTot = 0
    nCon = 0
    For i = 1 To UBound(b)
      If a(i, 3) = sCat And b(i, j) > 0 Then
        nTot = nTot + b(i, j)
        nCon = nCon + 1
      End If
    Next
    UserForm1.Controls("txtAve" & j).Value = nTot / nCon
  Next
End Sub

Now I have added more columns and I have AR as the last column ATM.

Instead of using only the unique items in column D to do the switches, I am adding AQ and AR.

Under AQ are strings in the form "Year1/Year2" eg 2020/2021
Then AR has strings like "LEVEL 1".

So under 2020/2021, the will or can be LEVEL 1, LEVEL 2, LEVEL 3 and so on.

Then the same thing could happen to 2021/2022 and so on.

So a criterion in column D, say X, having an ID of 7, could exit 3 times the number of different year ranges in column AQ as described above.

That's 2020/2021 will have
Code:
B      C            D     ......  AQ                   AR
ID    Name    Cat  ...... Year                  LEVEL
7     Ben 10   x      ...... 2020/2021      LEVEL 1
7     Ben 10   x      ...... 2020/2021      LEVEL 2
7     Ben 10   x      ...... 2020/2021      LEVEL 3

Then when we get to 2021/2022, the same ID will appear .

Which year range and LEVEL to handle is supplied by these two variable :

MyYear = "2020/2021"
MyLevel = "LEVEL 1"

As examples.

How do I tweak or amend the above code to get the job done for me?

Thanks in advance.
Kelly
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top