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.
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.
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.
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.
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With