Find max result of for loop to return which 'for'

dlee83

New Member
Joined
Mar 11, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,

Your help is greatly appreciated.
I'm working on something more complex but I need to find this result first to help me progress.

I have a simple for loop, running 24 combinations from 2 other subs. Each x result will return a value between 1-50 (tracknumber (which is a function that returns said number)).

How do I extract the highest/max result from this and with which 'x' it came from?

Public Sub longest()

Dim x As Integer

For x = 1 To 24
sub1
sub2
msgbox tracknumber
Next

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Assuming tracknumber produces the number you are trying to determine max value for, then in general, it would be something along these lines.

VBA Code:
Public Sub longest()
   
    Dim x As Integer, XMax As Integer
    Dim MaxTrackNumber As Variant
   
    For x = 1 To 24
        sub1
        sub2
        'MsgBox tracknumber
       
        If tracknumber > MaxTrackNumber Then
            MaxTrackNumber = tracknumber
            XMax = x
        End If
    Next x
   
    MsgBox "Max tracknumber is " & MaxTrackNumber & " at  x = " & XMax, vbOKOnly, Application.Name

End Sub

For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.

 
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

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