Error 1004: union failes

Tjordaske

New Member
Joined
Nov 23, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi,
I'm trying to split up an Excel in several excels based on a list of values in another Excel.
Let's show an example:

1639306627984.png


You see that in the top left Excel there is a list of values. My script starts with the first value 'binnendeuren' and search that value in the first 3 columns of the middle Excel. Each row that has that value in one of its first three columns is copied to a new Exel (bottom right)

Than my script should go to (and I think here it goes wrong) to the next value (grondwerken) and do the same thing again in the middle Excel:

1639306784784.png


The first step is working well but I think that there is a problem with the loop and/or broken link in which Excel he needs to look up the value and in which Excel he needs to find the rows with that value in the first three columns, because whatever I do I get every time an error. The current error I get is a 1004 error with the 'method union is failed' message:

1639306988845.png

1639307024951.png


I hope someone can help?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
VBA Code:
If rng is nothing then
Set rng=ocell.EntireRow
Else
Set rng=Union(rng,ocell.EntireRow)
End If
 
Upvote 0
You didn't qualify the Range("A:C") so it's implicit a range of the active sheet.
In case the rng variable refers to a range on another sheet than the active sheet (which cannot be determined at compile time) then VBA errors out in run-time as both ranges (rng & ocell) need to be on the same worksheet.
 
Upvote 0
Hi @GWteB,
I understand what you're saying but not how to solve this.
I changed the script (see yelow) to this, but still get's an error (see red)
1639381364613.png
 
Upvote 0
Try changing the line to:-
VBA Code:
Set rng = Union(rng, ActiveWorkbook.Worksheets(2).Rows(1))
 
Upvote 0
You also need to set Rng to Nothing before you move to the next iteration of the loop.
 
Upvote 0
Hi @RoryA and @Alex Blakenburg,

I've added the 'set rng = nothing' and I changed the line to this

1639392685639.png


But now it's creating the same Excel file over and over again, so it's not looking to the next value of the top left Excel (see original post) and maybe it's also not looking to the middle Excel as well (also see orginal post) because this is the result:

1639392649112.png


I was hoping by defining 'currentRange' and 'currentWS' in the beginning of the script that those values should remain fixed also when looping...
 
Upvote 0
Where do you set rng to Nothing? I can't see it.
 
Upvote 0
Hi @Tjordaske , are you able to post ALL of your code using code tags rather than an image?
I suspect your issue is due to the current loop logic.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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