Solved

Excel VBA 2010 - Determining row and Column sizes

Posted on 2013-12-15
3
361 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
Comment Utility
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 20

Accepted Solution

by:
Ejgil Hedegaard earned 250 total points
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now