GetSelectDim macro

xlf | GetSelectDim macro


GetSelectDim - about the macro


Description: returns the dimension (number of rows and number of columns) of the Excel selection.

The dimension information is displayed in a message box. Example output is shown in figure 1.


xlf-select-dim
Fig 1. About the selection message box - displaying the output from the GetSelectDim macro.


GetSelectDim - the VBA code



Code 1: Macro GetSelectDim returns the dimension of the Excel selection
Sub GetSelectDim()
Dim NoCols As Integer, NoRows As Long
Dim Srows As String, Scols As String

NoCols = Selection.Columns.Count
NoRows = Selection.rows.Count

    If NoCols = 1 Then
        Scols = " column"
    Else
        Scols = " columns"
    End If
    
    If NoRows = 1 Then
        Srows = " row x "
    Else
        Srows = " rows x "
    End If

    MsgBox "Selection dimension: " _
        & NoRows & Srows & NoCols & Scols _
        & "  [" & NoRows & "R X " & NoCols & "C]" _
        & vbNewLine & vbNewLine & "Coded by ExcelAtFinance " & Chr(169) & Chr(32) & Year(Date), _
        vbInformation, "About the selection"

End Sub