Solved

Excel VBA 2010 - Determining row and Column sizes

Posted on 2013-12-15
3
372 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

828 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