VBA Union not working consistenly

Lapwing

New Member
Joined
Jan 12, 2015
Messages
15
I am trying to scroll through each row of a range and copying row to a new range that match a certain criteria.
If each row matches the union works correctly. If there is a row that does not match the criteria then then next row that does match does not get joined by the union command -- even though the correct path through the code is followed.

Can I emphasise - the code follows the correct path, there is no error message but rngTemp does not increase in size -- the extra row is not added. Make all rows in rngSource fit the criteria and then the Union works as expected.

Can anyone explain?

Code:
For Each wbSource In Workbooks
If wbSource.Name <> "PERSONAL.XLSB" Then
    If Not wbSource.Name = wbTarget.Name Then
        Set wsSource = wbSource.Worksheets("Risk Log")
        Set rngSource = wsSource.UsedRange
            For Each rngRow In rngSource.Rows
                If Left(rngRow.Cells(1, 2), 3) = "TS:" Then
                    If Not rngTemp Is Nothing Then
                        Set rngTemp = Union(rngTemp, rngRow) ' this is the problem row
                    Else
                        Set rngTemp = rngRow
                    End If 'temp range is empty
                End If 'a TS row
            Next 'row
    End If 'target worksheet
End If 'personal.xlsb
 
I think the Address property of a range is limited, and the length of a literal range string is limited, but you can union a range to include any number of cells

Thanks, you're absolutely right of course.

I didn't even think to check this, but should have twigged given there was no error message. The .Address is truncated, but the range is correct.

So add: r.Value = "Hello", for example, and it works perfectly.
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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