Load part of data to 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 is the formula I am using to get data to second sheet.

Code:
Sheet2.Range("D7:M" & lr) = "=Sheet1!I7+Sheet1!S7*0.2"

Sheet1
SHEET_1.jpg


Sheet2
Screenshot_2020-02-17-00-20-36-1-1.png


Now what I want to do is to be able to load the portion of data from sheet2 for say category "X" into memory. Now I don't want to interact with the worksheet anymore. So looking at the formula as a guide, I want to perform those calculations in memory. Then I find the totals each row , D:M into N .

All in memory, if only that's possible.

If all the above go well, then what I want next is to be able to rank the total for ID 408 against the items in column N Sheet2 then display outcome with message box.

I wish and hope some can pull it up for me.

Thanks
 
You are very great.

All is working.

The only issue is that that choose function can only display from 1 to 4 ranks. 5th upwards adds nothing.

But in all, I love the code.

Have a wonderful time
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
The only issue is that that choose function can only display from 1 to 4 ranks. 5th upwards adds nothing.
Add in the next line the following letters of the rank
Rich (BB code):
MsgBox "The Rank of id: " & nID & " is: " & nRank & " " & Choose(nRank, "st", "nd", "rd", "th", "th", "th","th","etc")
 
Upvote 0
The only issue is that that choose function can only display from 1 to 4 ranks. 5th upwards adds nothing.

Try this:

VBA Code:
Sub test()
  Dim lr As Long, a As Variant, b As Variant, sufix As String
  Dim i As Long, j As Long, nSum As Double, nRank As Long
  Dim nID As Variant, nMax As Double, sCat As String, f As Range
  
  nID = 408
  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)
  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
      nMax = nSum
    End If
  Next
  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 Right(nRank, 1)
    Case "1": sufix = "st"
    Case "2": sufix = "nd"
    Case "3": sufix = "rd"
    Case Else: sufix = "th"
  End Select
  Sheet2.Range("D7").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  MsgBox "The Rank of id: " & nID & " is: " & nRank & " " & sufix
End Sub
 
Upvote 0
Ready with all requests:

VBA Code:
Sub test()
  Dim lr As Long, a As Variant, b As Variant
  Dim i As Long, j As Long, nSum As Double, nRank As Long
  Dim nID As Variant, nMax As Double, sCat As String, f As Range
  
  nID = 408
  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)
  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
      nMax = nSum
    End If
  Next
  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
  Sheet2.Range("D7").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  MsgBox "The Rank of id: " & nID & " is: " & nRank & " " & Choose(nRank, "st", "nd", "rd", "th")
End Sub

I just observed something :

The code is not giving me the right results.
The one from post#2 gave me right results.

This line
Code:
Sheet2.Range("D7").Resize(UBound(b, 1), UBound(b, 2)).Value = b

Values are scattered, not matching
 
Upvote 0
Oops. Sorry column H is blank.
The data we are working on is from I to AB.

I did it:

Code:
  For j = 8 To 17
      Select Case a(i, 3)
        Case "X", "Y", "Z"
          b(i, j - 7) = a(i, j) + a(i, j + 10) * 0.2
        Case Else
          b(i, j - 7) = a(i, j) + a(i, j + 10) * 0.1
      End Select
      nSum = nSum + b(i, j - 7)
    Next

Very grateful
 
Upvote 0
Try this:

VBA Code:
Sub test()
  Dim lr As Long, a As Variant, b As Variant, sufix As String
  Dim i As Long, j As Long, nSum As Double, nRank As Long
  Dim nID As Variant, nMax As Double, sCat As String, f As Range
  
  nID = 408
  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)
  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
      nMax = nSum
    End If
  Next
  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 Right(nRank, 1)
    Case "1": sufix = "st"
    Case "2": sufix = "nd"
    Case "3": sufix = "rd"
    Case Else: sufix = "th"
  End Select
  Sheet2.Range("D7").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  MsgBox "The Rank of id: " & nID & " is: " & nRank & " " & sufix
End Sub


Hi again @DanteAmor,

I have a situation here with me.

I am assigning the nID to a textbox input.

I click a listbox then the listbox loads the textbox

So nID = UserForm1.txtID.Value

So the goal was to able to switch the ranks as I click my listbox.

But what I am getting is that the display only switches the id values but not the ranks. And even the result produced for the first click is not correct.

I have tried setting f to nothing after the message line but still not getting through.

What could cause this? When I call the code from the user form with nID = 408, it displays the right result for the Id 408.

I just don't know why using the textbox value fails.
 
Upvote 0
Put all the code inside your userform. Create a button and on that button run the Test macro.

Change this:
nID = UserForm1.txtID.Value

For this:
nID = Val(UserForm1.txtID.Value)
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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