Join ranges in a single one given from comma separated string

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
328
Office Version
  1. 365
Platform
  1. Windows
Hi. I have this string that represents rows and row ranges separated by comma.

Code:
str = "1,3,6:8,11:12"

I want to join those rows in a single range variable (rng). I'm trying with this loop but the issue is initially rng is nothing and the "Union("nothing", Rows(s(0)))" gives error.

Code:
    For i = LBound(s) To UBound(s)
        Set rng = Union(rng, Rows(s(i)))
    Next

The complete code I have so far is:

Code:
Sub JoinRanges()
Dim str As String, s, rng As Range


str = "1,3,6:8,11:12"
s = Split(str, ",")

For i = LBound(s) To UBound(s)
    Set rng = Union(rng, Rows(s(i)))[COLOR=#ff0000] 'Here for first iteration I get error[/COLOR]
Next

rng.Select 

End Sub

How can be a way to do this?

Thanks for any help.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
Sub JoinRanges()
Dim str As String, s, rng As Range
str = "1,3,6:8,11:12"
s = Split(str, ",")
For i = LBound(s) To UBound(s)
    If rng Is Nothing Then
        Set rng = Rows(s(i))
    Else
        Set rng = Union(rng, Rows(s(i))) 'Here for first iteration I get error
    End If
Next i

rng.Select

End Sub
 
Upvote 0
Change the Set line:

Code:
    If Not rng Is Nothing Then
        Set rng = Union(rng, Rows(s(i)))
    Else
        Set rng = Rows(s(i))
    End If
 
Upvote 0
How about
Code:
For i = LBound(s) To UBound(s)
   If rng Is Nothing Then Set rng = Rows(s(i)) Else Set rng = Union(rng, Rows(s(i)))
Next
Or
Code:
Set rng = Rows(s(0))
For i = 1 To UBound(s)
   Set rng = Union(rng, Rows(s(i)))
Next
 
Upvote 0
Thank you all of you for the fast answer.

You solved my issue!

Very appreaciated.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top