Combine two ranges

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code combines two ranges.

It works if the ranges are on the same sheet but fails if they're not.

Code:
Dim rng1 As Range
  
    Set rng1 = Sheet1.Range("A1:A3")
  
    Dim rng2 As Range

    Set rng2 = Sheet2.Range("A1:A3")

    Dim rng3 As Range

    Set rng3 = Union(rng1, rng2)

    Dim rng3element As Range

    For Each rng3element In rng3

        Debug.Print rng3element.Value

    Next rng3element

How can I amend it so it would work?

Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Your welcome.

Just a question though, what exactly were you trying to achieve with that??
 
Upvote 0
Your welcome.

Just a question though, what exactly were you trying to achieve with that??
I'm trying to check to see if a particular value exists in either range 1 or range 2.

I'm doing it the long way using arrays instead.

I'm creating three arrays and adding values from the ranges.
 
Upvote 0
I'm trying to check to see if a particular value exists in either range 1 or range 2.

I'm doing it the long way using arrays instead.

I'm creating three arrays and adding values from the ranges.


try a workaround from the union method and see if it will do what you want?

VBA Code:
   Dim arrRng As Variant
   Dim rng(1 To 2) As Range
    Dim cell As Range

    Set rng(1) = Sheet1.Range("A1:A3")

    Set rng(2) = Sheet2.Range("B1:B3")


    For Each arrRng In Array(rng(1), rng(2))
        For Each cell In arrRng.Cells
            MsgBox cell.Parent.Name & " " & cell.Address
        Next cell
    Next arrRng

Dave
 
Upvote 0
try a workaround from the union method and see if it will do what you want?

VBA Code:
   Dim arrRng As Variant
   Dim rng(1 To 2) As Range
    Dim cell As Range

    Set rng(1) = Sheet1.Range("A1:A3")

    Set rng(2) = Sheet2.Range("B1:B3")


    For Each arrRng In Array(rng(1), rng(2))
        For Each cell In arrRng.Cells
            MsgBox cell.Parent.Name & " " & cell.Address
        Next cell
    Next arrRng

Dave
Not quite. I would need to adapt your code for my needs.

If Sheet1 contains the values 1, 2, 3 and Sheet2 contains, 4, 5, 6, I want the output to be in a single column, 1,2,3,4,5,6.

However, I've already done it using arrays.

Thanks for your help though.
 
Upvote 0
Not quite. I would need to adapt your code for my needs.

If Sheet1 contains the values 1, 2, 3 and Sheet2 contains, 4, 5, 6, I want the output to be in a single column, 1,2,3,4,5,6.

However, I've already done it using arrays.

Thanks for your help though.

without seeing your full code it was just a guess - but good you have it resolved.

Dave
 
Upvote 0
@tiredofit care to share your complete set of resulting code?


Code:
Dim Array1() As Variant
    
    Array1() = Sheet1.Cells(1, 1).CurrentRegion.Value
    
    Dim Array1Rows As Integer
    
    Array1Rows = UBound(Array1(), 1)
    
    Dim Array2() As Variant
    
    Array2() = Sheet2.Cells(1, 1).CurrentRegion.Value
    
    Dim Array2Rows As Integer
    
    Array2Rows = UBound(Array2(), 1)
    
    Dim CombinedArray() As Variant
    
    ReDim CombinedArray(1 To Array1Rows + Array2Rows, 1 To 1) As Variant
    
    Dim Counter As Integer
    
    For Counter = 1 To Array1Rows
    
        CombinedArray(Counter, 1) = Array1(Counter, 1)
        
    Next Counter
    
    For Counter = 1 To Array2Rows
    
        CombinedArray(Array1Rows + Counter, 1) = Array2(Counter, 1)
        
    Next Counter
    
    Sheet1.Cells(10, 10).Resize(Array1Rows + Array2Rows, 1).Value = CombinedArray()
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

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