• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

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.
0
Wilder1626
Asked:
Wilder1626
  • 2
  • 2
  • 2
1 Solution
 
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now