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

asked on

VB6 - Count number of selected cells in MSHFlexgrid1

Hi

I would like to know how i can count the number of cells i have selected in my MSHFlexgrid_1.

Normally, it would be a count of all the cells highlighted.

How can i do that?

Thanks again
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Selected in one row?
Avatar of Sha
Sha

Hi Wilder,
can you post a snap shot of selection and the expected answer (count of cells)?
Avatar of Wilder1626

ASKER

It could be a selection per row or per column

example in picture below

User generated image
Try this.
Dim MaxRow As Long
Dim MaxCol As Long
Dim FirstRow As Long
Dim FirstCol As Long
Dim intCount As Integer

With MSFlexGrid1
    FirstCol = .Col
    FirstRow = .Row
   MaxRow = .RowSel
   MaxCol = .ColSel

   Dim i, j As Integer

   For i = FirstRow To MaxRow
      For j = FirstCol To MaxCol
          intCount = intCount + 1
      Next
   Next
   
   MsgBox intCount
End With

Open in new window

SOLUTION
Avatar of Sha
Sha

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
Thanks Sha

MartinLiss

when i'm using code from ID: 40358522, it works but only if i select from left to right or top to bottom.

But if i select from right to left or bottom to top, it does not count.

Would you know why?

Private Sub MSHFlexGrid_1_GotFocus()
'selected_cell
Dim MaxRow As Long
Dim MaxCol As Long
Dim FirstRow As Long
Dim FirstCol As Long
Dim intCount As Integer

With MSHFlexGrid_1
    FirstCol = .Col
    FirstRow = .Row
   MaxRow = .RowSel
   MaxCol = .ColSel

   Dim i, j As Integer

   For i = FirstRow To MaxRow
      For j = FirstCol To MaxCol
          intCount = intCount + 1
      Next
   Next
   
   selected_cell = intCount
End With

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
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
Thanks again for your help. This is working great.

Sorry for the delay.
No problem. glad I could help.