Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6 - Count number of selected cells in MSHFlexgrid1

Posted on 2014-10-02
9
Medium Priority
?
111 Views
Last Modified: 2014-10-05
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
0
Comment
Question by:Wilder1626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40358497
Selected in one row?
0
 

Expert Comment

by:Sha
ID: 40358501
Hi Wilder,
can you post a snap shot of selection and the expected answer (count of cells)?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 40358507
It could be a selection per row or per column

example in picture below

Count cells when selected
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:Martin Liss
ID: 40358522
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

0
 

Assisted Solution

by:Sha
Sha earned 300 total points
ID: 40358530
You can use ColSel, RowSel properties to select a specific region of the MSHFlexGrid programmatically, or to read the dimensions of an area that you selects into code.

The cursor is in the cell at Row, Col. The selection is the region between rows Row and RowSel and columns Col and ColSel. Note that RowSel may be above or below Row, and ColSel may be to the left or to the right of Col.

so you will have to do a calculation to determine the number of selected rows or cols by (higher value - lower value +1)

once you have no. of rows and no. of cols, u can multiply for the cells.

Whenever you set the Row and Col properties, RowSel and ColSel are automatically reset, so the cursor becomes the current selection.

even you can select a block of cells from code, you must first set the Row and Col properties, and then set RowSel and ColSel.

hope this helps.
Sha
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 40360621
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

0
 
LVL 49

Accepted Solution

by:
Martin Liss earned 1700 total points
ID: 40360658
Do it this way instead.

Dim MaxRow As Long
Dim MaxCol As Long
Dim FirstRow As Long
Dim FirstCol As Long
Dim intCount As Integer
Dim intRow As Integer
Dim intCol As Integer

With MSFlexGrid1
    If .RowSel > .Row Then
        FirstRow = .Row
        MaxRow = .RowSel
    Else
        FirstRow = .RowSel
        MaxRow = .Row
    End If
    If .ColSel > .Col Then
        FirstCol = .Col
        MaxCol = .ColSel
    Else
        FirstCol = .ColSel
        MaxCol = .Col
    End If
    
    For intRow = FirstRow To MaxRow
       For intCol = FirstCol To MaxCol
           intCount = intCount + 1
       Next
    Next
    
    MsgBox intCount
End With

Open in new window

0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 40362596
Thanks again for your help. This is working great.

Sorry for the delay.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40362646
No problem. glad I could help.
0

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question