Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether an Excel Range represents entire row or column

Tags:

range

excel

vba

I need to determine if a given Range object is actully an entire column OR entire row OR none of two.

What I do now is based on the next code which works for me.

If Range.Rows.Count = Range.EntireColumn.Rows.Count Then
...
ElseIf Range.Columns.Count = Range.EntireRow.Columns.Count Then
...
End If

I'd like to know if there's more efficient and/or elegant way of doing the same? Perhaps, some built-in property that I've overlooked?

UPD: I get the range from:

Worksheet.Range("NamedRange")

Thanks.

like image 457
hypers Avatar asked Oct 28 '16 10:10

hypers


People also ask

How do you create a row range in Excel?

For example, Range("A1:B2"). Rows(5) returns cells A5:B5. For more information, see the Item property. Using the Rows property without an object qualifier is equivalent to using ActiveSheet.

How do you find the number of rows in a range?

ROWS counts the number of rows in any supplied range and returns a number as a result. For example, if we provide all of column A in a range, Excel returns 1,048,576 the total number of rows in an Excel worksheet. To count columns in a range, see the COLUMNS function.

What is column and row range?

The Columns and Rows properties refer to the columns and rows of a specified Range object, which can be a worksheet or a range of cells. They return a Range object referencing the rows or columns of the specified object.

How to check if a range contains a specific value by row?

If a range contains a specific value by row using VBA. Output Range: Select the output range by changing the cell reference ("E8") in the VBA code. Range to Test: Select the range that you want to search through for a specific value by changing the range reference ("C8:C14") in the VBA code.

How to select all cells in a range in Excel?

In the formula, A2:A6 is the range you want to use, B2 is the value you want to check if appears in the range. 2. This formula also can use to a range that each cell contains one data. The select Specific Cells of Kutools for Excel can quicky select all cells or rows or columns in a range based on one criterion or two criterion.

How to search for a specific value by row in Excel VBA?

METHOD 1. If a range contains a specific value by row using VBA Output Range: Select the output range by changing the cell reference ("E8") in the VBA code. Range to Test: Select the range that you want to search through for a specific value by changing the range reference ("C8:C14") in the VBA code.

How to check if appears in a range in Excel?

Select a blank cell next to the value you want to check if appears in the range, type this formula =COUNTIF ($A$2:$A$6,"*"&B2&"*")>0, and drag the auto fill handle down to check other values. See screenshot: 1. In the formula, A2:A6 is the range you want to use, B2 is the value you want to check if appears in the range.


2 Answers

Since I think the OP's own question contains a great answer (for most uses) and three years later no one has posted it, here it is. I converted the same logic into these two functions:

Function IsEntireRow(ByVal Target As Excel.Range) As Boolean
    IsEntireRow = Target.Columns.Count = Target.EntireRow.Columns.Count
End Function

Function IsEntireColumn(ByVal Target As Excel.Range) As Boolean
    IsEntireColumn = Target.Rows.Count = Target.EntireColumn.Rows.Count
End Function
like image 73
ChrisB Avatar answered Sep 30 '22 08:09

ChrisB


This will work in all Excel versions:

Function cellsInRange(rng As Range) As Boolean

    Dim cellsInCol As Long, cellsInRow As Long, cols As Long, count As Long, rws As Long

    With ActiveSheet
        cellsInCol = .Columns("A").Cells.count
        cellsInRow = .Rows(1).Cells.count
    End With

    With rng
        cols = .Columns.count
        count = .Cells.count
        rws = .Rows.count
    End With

    If cols = 1 And count = cellsInCol Then
        cellsInRange = True
    ElseIf rws = 1 And count = cellsInRow Then
        cellsInRange = True
    End If

End Function

Edit

If more specific details are required about the range object, the following adaptation may prove useful:

Function cellsInRange(rng As Range) As String

    Dim cellsInCol As Long, cellsInRow As Long, cols As Long, count As Long, rws As Long

    With ActiveSheet
        cellsInCol = .Columns("A").Cells.count
        cellsInRow = .Rows(1).Cells.count
    End With

    With rng
        cols = .Columns.count
        count = .Cells.count
        rws = .Rows.count
    End With

    If cols = 1 And count = cellsInCol Then
        cellsInRange = "Single column"
    ElseIf rws = 1 And count = cellsInRow Then
        cellsInRange = "Single row"
    Else
        cellsInRange = "Neither single column nor single row"
    End If

End Function
like image 33
Miqi180 Avatar answered Sep 30 '22 06:09

Miqi180