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?
 
Here is the code:
VBA Code:
Sub TestLoopInLoop()
Dim ocell As Range
Dim rng As Range
Dim FileName As String
Dim FilePath As String
Dim x As String
Dim i As Integer
Dim currentRange As Range
Dim currentWS As Worksheet
Dim newWB As Workbook
Dim newS As Worksheet
Dim Naam As Variant

Set currentRange = ActiveWorkbook.Worksheets(2).Range("A:C")
Set currentWS = ActiveWorkbook.Worksheets(2)

i = 1
x = Workbooks("Overzicht categorieën.xlsx").Sheets(1).Cells(i, 1).Value

Do While Not IsEmpty(Workbooks("Overzicht categorieën.xlsx").Sheets(1).Cells(i, 1))

    For Each ocell In currentRange
    If ocell.Value = x Then
        If rng Is Nothing Then
            Set rng = ocell.EntireRow
        Else
            Set rng = Union(rng, ocell.EntireRow)
        End If
    End If
    Next ocell
    Set rng = Union(rng, currentWS.Rows(1))
    rng.EntireRow.Copy
   
    FilePath = ActiveWorkbook.Path
    FileName = ActiveWorkbook.Name
    If InStr(FileName, ".") > 0 Then
        FileName = Left(FileName, InStr(FileName, ".x") - 1)
    End If
    Naam = InputBox("Onder welke naam moet het bestand opgeslagen worden?", "Meetstaat Wyckaert", FileName & " - " & x)
   
    Set newWB = Workbooks.Add
        With newWB
            Set newS = newWB.Sheets(1)
            newS.Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
            newS.Range("A1").PasteSpecial Paste:=xlPasteFormats
            newS.Range("A1").PasteSpecial Paste:=xlPasteValues
            .SaveAs FileName:=FilePath & "\" & Naam
        End With

    i = 1 + 1
    Set rng = Nothing
    Loop

End Sub
 
Last edited by a moderator:
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Shouldn't this x = Workbooks("Overzicht categorieën.xlsx").Sheets(1).Cells(i, 1).Value, repeat at the end of the Do Loop ?

VBA Code:
        i = 1 + 1
        x = Workbooks("Overzicht categorieën.xlsx").Sheets(1).Cells(i, 1).Value
        Set rng = Nothing
    Loop

and the Do loop start with:
VBA Code:
Do While x <> ""
 
Upvote 0
Solution
Almost there... Now the first two times goes well, but the third time it's splitting up the Excel it's a copy of the second run.

Here is the code:
VBA Code:
Sub TestLoopInLoop()
Dim ocell As Range
Dim rng As Range
Dim FileName As String
Dim FilePath As String
Dim x As String
Dim i As Integer
Dim currentRange As Range
Dim currentWS As Worksheet
Dim newWB As Workbook
Dim newS As Worksheet
Dim Naam As Variant

Set currentRange = ActiveWorkbook.Worksheets(2).Range("A:C")
Set currentWS = ActiveWorkbook.Worksheets(2)

i = 1
x = Workbooks("Overzicht categorieën.xlsx").Sheets(1).Cells(i, 1).Value

Do While x <> ""

    For Each ocell In currentRange
    If ocell.Value = x Then
        If rng Is Nothing Then
            Set rng = ocell.EntireRow
        Else
            Set rng = Union(rng, ocell.EntireRow)
        End If
    End If
    Next ocell
    Set rng = Union(rng, currentWS.Rows(1))
    rng.EntireRow.Copy
   
    FilePath = ActiveWorkbook.Path
    FileName = ActiveWorkbook.Name
    If InStr(FileName, ".") > 0 Then
        FileName = Left(FileName, InStr(FileName, ".x") - 1)
    End If
    Naam = InputBox("Onder welke naam moet het bestand opgeslagen worden?", "Meetstaat Wyckaert", FileName & " - " & x)
   
    Set newWB = Workbooks.Add
        With newWB
            Set newS = newWB.Sheets(1)
            newS.Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
            newS.Range("A1").PasteSpecial Paste:=xlPasteFormats
            newS.Range("A1").PasteSpecial Paste:=xlPasteValues
            .SaveAs FileName:=FilePath & "\" & Naam
        End With
   
    i = 1 + 1
    x = Workbooks("Overzicht categorieën.xlsx").Sheets(1).Cells(i, 1).Value
    Set rng = Nothing
    Loop

End Sub
 
Last edited by a moderator:
Upvote 0
The third run/loop should only be the rows that contains the word 'pleisterwerken' in column A, B or C.

Almost there.jpg
 
Upvote 0
Typo - it should be:

VBA Code:
i = i + 1

not:

VBA Code:
i = 1 + 1

:)
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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