Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

Biggest number in column 2 of a MSFlexgrid

Hello all,

I would like, by a click on a button, look at the biggest number entered in the second column of MSFLexgrid1, and then, put that number in my Label1.

The numbers are not in sequence.

How can i do this please?

Thanks for your help.
Avatar of bingie
bingie

If your flexgrid is named "fg", your button is named "cmd1" and your label is named "lbl1" then use this function and code in your button's click event:

Private Sub cmd1_Click()
lbl1.Caption = findLargestItemInFlexGridColumn(fg, 1)
End Sub

Private Function findLargestItemInFlexGridColumn(fg As MSFlexGrid, fgCol As Integer) As Double
Dim i As Integer
Dim maxValue As Double
maxValue = fg.TextMatrix(0, fgCol)

For i = 0 To fg.Rows - 1
If CDbl(fg.TextMatrix(i, fgCol)) > maxValue Then maxValue = CDbl(fg.TextMatrix(i, fg.Col))
Next i

findLargestItemInFlexGridColumn = maxValue
End Function
With formatting...
Private Sub cmd1_Click()
   lbl1.Caption = findLargestItemInFlexGridColumn(fg, 1)
End Sub

Private Function findLargestItemInFlexGridColumn(fg As MSFlexGrid, fgCol As Integer) As Double
Dim i As Integer
Dim maxValue As Double
maxValue = fg.TextMatrix(0, fgCol)

For i = 0 To fg.Rows - 1
   If CDbl(fg.TextMatrix(i, fgCol)) > maxValue Then maxValue = CDbl(fg.TextMatrix(i, fg.Col))
Next i

findLargestItemInFlexGridColumn = maxValue
End Function

Open in new window

Avatar of Wilder1626

ASKER

Hello,

I have a type incompatible with that part of the code:
maxValue = fg.TextMatrix(0, fgCol)

Do you know why?

Thanks again for your help.
ASKER CERTIFIED SOLUTION
Avatar of Chris Raisin
Chris Raisin
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perhaps the column number should be 1, not  2, sorry.
The columns are numbered from zero to "cols-1", so the second column would be "column 1"
Therefor the "Click" sub  should read:
Private Sub cmdCommand1_Click()
    lblLabel1.Caption = findLargestItemInFlexGridColumn(MSFlexGrid1, 1)
End Sub  
 
I think you would find this faster than performing a "For Next" loop which invloves calculation at each looping. Array sorts are much faster.
Oh yessss,

Thanks, it work.