Solved

Excel VBA 2010 - Determining row and Column sizes

Posted on 2013-12-15
3
370 Views
Last Modified: 2013-12-16
My active cell is inside an island of cells somewhere on my worksheet and I would like to know:
1.  how many rows  and how many columns this region consists of
2.  I'd like a message box saying "Your island consists of 9 rows by 10 columns"
How can I do this in VBA?
0
Comment
Question by:brothertruffle880
3 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39719848
What do you mean by Island of cells. Is it a range that is selected (Highlighted)  and the current cell is somewhere in the selected region?
0
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 250 total points
ID: 39720383
Insert below code in the Workbook module (ThisWorkbook), and doubleclick on a cell in the region of cells.

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    MsgBox "Your island consist of" + Str(ActiveCell.CurrentRegion.Rows.Count) + " rows by" + Str(ActiveCell.CurrentRegion.Columns.Count) + " columns", vbOKOnly, "Island count"
End Sub

Open in new window

0
 
LVL 14

Assisted Solution

by:Faustulus
Faustulus earned 250 total points
ID: 39721006
Please try this code. It should be installed in the code sheet of the tab on which you wish to have the action. Right-click on the tab and select "View code" to open the correct sheet, then use Copy/Paste to install the code.
Option Explicit

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

    Dim Msg As String
    Dim R As Long
    Dim C As Long
    
    With Target
        If .Cells.Count > 1 Then
            R = .Rows.Count
            C = .Columns.Count
            Msg = "Your current selection has" & vbCr & _
                  String(6, " ") & R & " row" & _
                  IIf(R = 1, "", "s") & " and" & vbCr & _
                  String(6, " ") & C & " column" & _
                  IIf(C = 1, ".", "s.")
            MsgBox Msg, vbInformation, "Selected range"
            Cancel = True
        End If
    End With
End Sub

Open in new window

This code will be activated by right-clicking. It will still allow you access to the normal functions of the right-click if only a single cell is selected. If several cells are selected the message box you asked for will be displayed.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

821 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