Union Ranges on two different sheets

hydrojoe11

New Member
Joined
Feb 25, 2008
Messages
18
Hello all - I'm trying to union two different ranges that exist on two different sheets and then copy the unique values into a third range with just the unique values. I was going to then use the unique range as my rowsource in a listbox. This is the code I was working with so far but i'm getting a compile error (method range of object failure). Any ideas



Code:
Private Sub UserForm_Initialize()
Dim range4 As Range
Dim range3 As Range
Dim range2 As Range
Dim range1 As Range
Set range1 = Worksheets(1).Range("MyRange")
Set range2 = Worksheets(2).Range("MyRange2")
Application.Union(Range("range1"), Range("range2")).Select
Selection = range3
range3.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=range4, Unique:=True
Me.lstone.RowSource = range4
End Sub
 
Sure

First range is 2 columns

bob 5
cat 3
dog 4

second range is 2 columns

bob 5
cat 7
dog 19

The final range should be contain these values

cat 3
cat 7
dog 4
dog 19

and the row with bob and 5 should be removed on a double match essentially.
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Ok. Take one column of data, move it to the bottom of the other, then do another AdvancedFilter on it. If you have headers in row 1 (I'm assuming), then maybe use...



<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> wks3 <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> Rng1 <SPAN style="color:#00007F">As</SPAN> Range, Rng2 <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Set</SPAN> wks3 = Worksheets(3)<br>    <SPAN style="color:#00007F">Set</SPAN> Rng1 = Worksheets(1).Range("Range1")<br>    <SPAN style="color:#00007F">Set</SPAN> Rng2 = Worksheets(2).Range("Range2")<br>    wks3.Range("A1").Resize(Rng1.Rows.Count, Rng1.Columns.Count).Value = Rng1.Value<br>    wks3.Range("C1").Resize(Rng2.Rows.Count, Rng2.Columns.Count).Value = Rng2.Value<br>    wks3.Range("C2", wks3.Cells(wks3.Rows.Count, 4).End(xlUp)).Cut wks3.Cells(wks3.Rows.Count, 1).End(xlUp).Offset(1)<br>    Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN><br>    wks3.Range("C:D").Delete<br>    <SPAN style="color:#00007F">With</SPAN> wks3.Range("A1", wks3.Cells(wks3.Rows.Count, 2).End(xlUp))<br>        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    wks3.Range("A2", wks3.Cells(wks3.Rows.Count, 2).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, copytorange:=wks3.Range("C1"), unique:=<SPAN style="color:#00007F">True</SPAN></FONT>



Tested roughly on some data, seemed to work for me. You'll end up with no headers (not sure if that is what you wanted or not).
 
Upvote 0
Here's an alternative:

1. Make sure your data tables have headings (eg Name and Value)
2. Name the data tables "data1" and "data2" (without the quotes).
3. Save the spreadsheet
4. Select a fresh worksheet in the workbook and go Data>Import External Data>New Database Query>Excel and select the saved file name from wherever it is located. You will probably be asked to add tables, and you want to add "data1" and "data2".
5. This should take you into MSQuery. In MSQuery, click on the SQL button below the menu buttons at top left.
6. Copy in the following SQL to the SQL dialog (amend the workbook name and path to whatever is appropriate):
Code:
SELECT data1.Animal, data1.Value 
FROM `C:\SQLtest.xls`.data1 data1
WHERE data1.Animal & data1.Value Not In(Select data2.Animal & data2.Value FROM `C:\SQLtest.xls`.data2 data2)
UNION ALL
SELECT data2.Animal, data2.Value 
FROM `C:\SQLtest.xls`.data2 data2
WHERE data2.Animal & data2.Value Not In(Select data1.Animal & data1.Value FROM `C:\SQLtest.xls`.data1 data1)

Note the back quotes around the file name.
7. Click OK and go File>Return Data to Microsoft Excel
 
Upvote 0
Thanks for the code...that's way over my head though to be honest :) I'm still learning to be efficient with VBA, this small part is part of a larger program actually which is why i need it to function this way.
 
Upvote 0
Okay, I see why it's bringing both over. They are in fact two different cell values. The second 'bob' has trailing spaces, which it picks up. To fix this, get rid of your trailing spaces (space characters put at the end of a value in the cells, they appear blank).

An easy way to fix this is 1) run your own code on the values, or 2) get ASAP Utilities (www.asap-utilities.com) and it has a routine which will clear the leading/trailing spaces of a selection of cells. These lead to many problems. Clear those spaces and it runs just fine.

Also, I did not realize you did not have any headers, this would change ever so slightly (some rows changed from 2 to 1) and I added a couple lines of deleting columns to clean things up a bit. This works with your presented data (assuming you've cleared the trailing spaces)...




<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> wks3 <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> Rng1 <SPAN style="color:#00007F">As</SPAN> Range, Rng2 <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Set</SPAN> wks3 = Worksheets(3)<br>    <SPAN style="color:#00007F">Set</SPAN> Rng1 = Worksheets(1).Range("Range1")<br>    <SPAN style="color:#00007F">Set</SPAN> Rng2 = Worksheets(2).Range("Range2")<br>    wks3.Range("A:D").Delete<br>    wks3.Range("A1").Resize(Rng1.Rows.Count, Rng1.Columns.Count).Value = Rng1.Value<br>    wks3.Range("C1").Resize(Rng2.Rows.Count, Rng2.Columns.Count).Value = Rng2.Value<br>    wks3.Range("C1", wks3.Cells(wks3.Rows.Count, 4).End(xlUp)).Cut wks3.Cells(wks3.Rows.Count, 1).End(xlUp).Offset(1)<br>    Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN><br>    wks3.Range("C:D").Delete<br>    <SPAN style="color:#00007F">With</SPAN> wks3.Range("A1", wks3.Cells(wks3.Rows.Count, 2).End(xlUp))<br>        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    wks3.Range("A1", wks3.Cells(wks3.Rows.Count, 2).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, copytorange:=wks3.Range("C1"), unique:=<SPAN style="color:#00007F">True</SPAN><br>    wks3.Range("A:B").Delete</FONT>



Let us know how it works.
 
Upvote 0
YAY - Thanks for all your help, I removed the trailing spaces and it did exactly what I wanted to. I appreciate you sticking with me and helping me solve this issue.

This is a great forum.

Joe
 
Upvote 0

Forum statistics

Threads
1,216,989
Messages
6,133,888
Members
449,843
Latest member
TheLastLinuxUser

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