How to combine multiple non-adjacent ranges in one?

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

I have these 4 ranges that I'd like to combine in one, I've tried union but didn't worked. So I've found this function that in theory does the job, however not sure how to write the final range back to the sheet. Any suggestions?

1626778408488.png


VBA Code:
Sub CombininingArrays()

Dim Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range

  Set Rng1 = Sheets("Summary").Range("e5:e18")
  Set Rng2 = Sheets("Summary").Range("o6:p18")
  Set Rng3 = Sheets("Summary").Range("x6:y18")
  Set Rng4 = Sheets("Summary").Range("Ag5:am18")
 
  Dim CombinedRange As Collection
  Set CombinedRange = New Collection
  Dim Vartable() As Variant
   
  CombinedRange.Add Rng1
  CombinedRange.Add Rng2
  CombinedRange.Add Rng3
  CombinedRange.Add Rng4
 
  'transfer combinedRange to array using function CollectionToArray
   Vartable = CollectionToArray(CombinedRange)
 
End Sub

 Function CollectionToArray(col As Collection) As Variant()
    Dim arr() As Variant, index As Long, it As Variant
    ReDim arr(col.Count - 1) As Variant
    For Each it In col
        arr(index) = it
        index = index + 1
    Next it
    CollectionToArray = arr
 End Function
 
Hi @Peter_SSs ,

Do you mind asking how did you find this solution using the array and index function? I really liked it. The only limitation I see on that, is that all ranges needs to be within the gap 5:18, correct?

VBA Code:
Sub CombineRangesToArray()
Dim Vartable As Variant
 
    With Sheets("Summary")
    Vartable = Application.Index(Cells, Evaluate("row(5:18)"), Array(5, 15, 16, 24, 25, 33, 34, 35, 36, 37, 38, 39))
    .Range("BA1").Resize(UBound(Vartable, 1), UBound(Vartable, 2)).Value = Vartable
    End With

End Sub


cheers,
Gill
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Do you mind asking how did you find this solution using the array and index function?
This technique of loading multiple disjoint ranges into an array I came across in the forum many years ago

The only limitation I see on that, is that all ranges needs to be within the gap 5:18, correct?
The dimensions of an array are uniform so the rows/columns feeding into it also need to be uniform if they are all being done at once. If you wanted some cell values omitted as it appears from your original code then you would need to process the ranges separately or else put everything in at once per code like mine and then remove the unwanted values.
 
Upvote 0
This technique of loading multiple disjoint ranges into an array I came across in the forum many years ago


The dimensions of an array are uniform so the rows/columns feeding into it also need to be uniform if they are all being done at once. If you wanted some cell values omitted as it appears from your original code then you would need to process the ranges separately or else put everything in at once per code like mine and then remove the unwanted values.
Thanks one more time @Peter_SSs! Wow! You got that solution from 2011! Time is flying...hehe
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,151
Members
449,068
Latest member
shiz11713

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