Mierzen
Mierzen

Reputation: 616

Excel VBA: Determine if range is horizontal or vertical

I want to create a function that, among others, has a range as input. This range will be 1 row by ? columns or 1 columns by ? rows. In other words, it will be "one dimensional" - either horizontal or vertical.

Based on whether it is H or V, certain calculations will be made. How can I determine if the range that is passed is horizontal or vertical?

Upvotes: 3

Views: 2911

Answers (2)

rory.ap
rory.ap

Reputation: 35280

If Range.Columns.Count=1 then it's vertical. If Range.Rows.Count=1, it's horizontal (assuming, like you said, you only have either rows or columns).

Upvotes: 1

Alex P
Alex P

Reputation: 12487

How about this:

Function RangeIsVertical(rng As Range) As Boolean
    RangeIsVertical = IIf(rng.Columns.Count = 1, 1, 0)
End Function

Sub Test()
    Debug.Print RangeIsVertical(Range("A1")) //True
    Debug.Print RangeIsVertical(Range("A1:A10")) //True
    Debug.Print RangeIsVertical(Range("A1:B2")) //False
End Sub

Upvotes: 4

Related Questions