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.
LVL 11
Wilder1626Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bingieCommented:
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
0
bingieCommented:
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

0
Wilder1626Author Commented:
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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Chris Raisin(Retired Analyst/Programmer)Commented:
You may find the following code much quicker.
Option Explicit

Private Sub cmdCommand1_Click()
lblLabel1.Caption = findLargestItemInFlexGridColumn(MSFlexGrid1, 2)
End Sub

Private Function findLargestItemInFlexGridColumn( _
                    fg As MSFlexGrid, fgCol As Integer) As Double
Dim i As Integer
Dim maxValue As Double
Dim Vals() As Double
ReDim Vals(fg.Rows - 1)
For i = 0 To (fg.Rows - 1)
  Vals(i) = CDbl(Val(fg.TextMatrix(i, fgCol)))
Next i
SortArray Vals
findLargestItemInFlexGridColumn = Vals(UBound(Vals))
End Function

Public Sub SortArray(ByRef dblArray() As Double)
  Dim lsHold As String
  Dim i      As Integer
  Dim x      As Integer
 
  For i = LBound(dblArray) + 1 To UBound(dblArray)
    lsHold = dblArray(i)
    For x = i - 1 To LBound(dblArray) Step -1
      If lsHold >= dblArray(x) Then
        Exit For
      End If
      dblArray(x + 1) = dblArray(x)
    Next x
    dblArray(x + 1) = lsHold
  Next i
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris Raisin(Retired Analyst/Programmer)Commented:
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.
0
Wilder1626Author Commented:
Oh yessss,

Thanks, it work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.