blackrock
02-15-2013, 09:22 PM
Hi Could someone please help me understand this loop. It joins two or more ranges together as it loops. Thanks
Sub a()
Dim LR As Long, cell As Range, rng As Range, cells As Range
Dim newrange As Range
Dim counter As Variant
For Each cell In Sheet3.Range("ServiceDates")
If cell.Value <> "" Then
If rng Is Nothing Then
Set rng = cell
Else
Set rng = Union(rng, cell)
End If
End If
Next cell
rng.Select
End Sub
I think the first time it loops the first if statment is true.
For the rest of the times it loops first if statement is false else is true.
Is this correct and how does Set rng = Union(rng, cell) work. cheers
Sub a()
Dim LR As Long, cell As Range, rng As Range, cells As Range
Dim newrange As Range
Dim counter As Variant
For Each cell In Sheet3.Range("ServiceDates")
If cell.Value <> "" Then
If rng Is Nothing Then
Set rng = cell
Else
Set rng = Union(rng, cell)
End If
End If
Next cell
rng.Select
End Sub
I think the first time it loops the first if statment is true.
For the rest of the times it loops first if statement is false else is true.
Is this correct and how does Set rng = Union(rng, cell) work. cheers