Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and use empty range in VBA?

I would like to use empty range in following manner :

Set NewRange = Union(EmptyRange, SomeRange)

I've tried to set EmptyRange as empty range using Nothing, Empty and Null but "run-time error '5' Invalid procedure call or argument" occurs, it seems that I have to use If statement or there is other keyword which do the job ?

I can use :

If EmptyRange Is Nothing Then
   Set NewRange = SomeRange
Else
   Set NewRange = Union(EmptyRange, SomeRange)
End If

instead of construction:

Set NewRange = Union(EmptyRange, SomeRange)
like image 946
Qbik Avatar asked Dec 18 '14 20:12

Qbik


People also ask

How do you set a range in VBA?

To set the reference, we need to use the “SET” keyword and enter the cell addresses using the RANGE object. The variable “Rng” refers to the cells A1 to B5. Instead of writing the cell address Range (“A1:B5”), we can use the variable name “Rng.” It will insert the mentioned value from the A1 to the B5 cells.

How do I use blank in excel VBA?

VBA IsEmpty is a logical function that tests whether the selected is empty or not. Since it is a logical function, it will return the results in Boolean values, i.e., TRUE or FALSE. If the selected cell is empty, it will return TRUE, or else it will return FALSE.

How do you refer to an empty cell in VBA?

With a Worksheet Cell If you wish to test whether a worksheet cell is empty in VBA, you can not use the worksheet function called ISBLANK. In VBA, you must use the ISEMPTY function. In this example, we will test whether cell A1 is empty. If cell A1 is empty, the message "Cell A1 is empty" will be displayed.

How do you assign a range to a range variable in VBA?

Use the “Dim” keyword and enter the name of the variable. Declare the keyword as Range. Type the name of the variable. Specify the range that you want to assign to the variable.


1 Answers

I use this function as a replacement for Application.Union when I need to combine several range objects, where "zero or more" of the ranges might be Nothing:

Function union(ParamArray rgs() As Variant) As Range
  Dim i As Long
  For i = 0 To UBound(rgs())
    If Not rgs(i) Is Nothing Then
      If union Is Nothing Then Set union = rgs(i) Else Set union = Application.union(union, rgs(i))
    End If
  Next i
End Function

Example Usage:

Sub demo_union()
  Dim rg1 As Range, rg2 As Range, rg3 As Range, newRg As Range
  Set rg1 = Range("A1")
  Set rg3 = Range("C3")
  Set newRg = union(rg1, rg2, rg3)
  newRg.Select
End Sub

Below is a variation that does not duplicate overlapping cells in the returned range.

Normally when combining overlapping ranges (eg., A1:B2 and B2:C3) with Application.Union (or the function above), the result will have multiple copies of the overlapping cells.

For example using,
overlapping ranges A1:B2 and B2:C3

Application.Union([A1:B2], [B2:C3]).Cells.Count '8 cells (repeats B2)

↑ ...returns 8 cells: A1 B1 A2 B2 B2C2 B3 C3
(and a For Each loop will have 8 iterations.)

Function union2 (below) solves this issue by returning only unique cells, and also handles empty ranges (without producing an annoyingly-vague "Invalid Procedure call or argument")

Debug.Print union2([A1:B2], [B2:C3]).Cells.Count '7 cells

↑ ...returns 7 cells: A1 B1 A2 B2 C2 B3 C3
(For Each loop will have 7 iterations.)

like image 83
ashleedawg Avatar answered Sep 18 '22 16:09

ashleedawg