Convert a combination of ranges into only one range

radames.rios

New Member
Joined
Nov 30, 2012
Messages
11
I have a range in VBA that says for example:

(A1, A2, A3, A4, A6, A7, A8, A13, A15)

Is it possible to merge that range together and simplify it into:

(A1:A4, A6:A8, A13, A15)

Note that the range covers cells from A1 to A15 but excluding some of the cells in that set.

There is no specific pattern when selecting the cells.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
What I meant was that the result I wanted was the simplified version:

"A1:A4,A6:A8,A13,A15"
If you have those addresses as a comma delimited text string, then I think this function does what you want...
Code:
Function Shorten(ByVal S As String) As String
  Shorten = Intersect(Cells, Range(S)).Address(0, 0)
End Function
Or, if you are selecting the cells, then this function instead...
Code:
Function Shorten() As String
  Shorten = Intersect(Cells, Selection).Address(0, 0)
End Function
 
Last edited:
Upvote 0
Hi

... and another way:

Code:
Sub Test()
Dim r As Range

Set r = Range("A1, A2, A3, A4, A6, A7, A8, A13, A15")
Set r = Union(r, r)

MsgBox "r: " & r.Address
End Sub
 
Upvote 0
If you have those addresses as a comma delimited text string, then I think this function does what you want...
Code:
Function Shorten(ByVal S As String) As String
  Shorten = Intersect(Cells, Range(S)).Address(0, 0)
End Function
Or, if you are selecting the cells, then this function instead...
Code:
Function Shorten() As String
  Shorten = Intersect(Cells, Selection).Address(0, 0)
End Function
I am not sure if you missed my response (shown above) or if you did not want a function-type solution, so I have recast the code above into macro form which may be the way you wanted to see it...
Code:
Sub Test()
  MsgBox Intersect(Cells, Range("A1, A2, A3, A4, A6, A7, A8, A13, A15")).Address(0, 0)
End Sub
If you wanted to see it in a less compacted and perhaps more usable form for you...
Code:
Sub Test2()
  Dim Addresses As String, ShortenedAddresses As String
  
  Addresses = "A1, A2, A3, A4, A6, A7, A8, A13, A15"
  ShortenedAddresses = Intersect(Cells, Range(Addresses)).Address(0, 0)
  
  MsgBox ShortenedAddresses
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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