VBA: method union of object _global failed

kera404

New Member
Joined
Jun 7, 2016
Messages
8
I am trying to filter out the rows having the cells in a particular column (the 'O'th column) with the value "Desired". For this I've written this code

Code:
Set PTRange = ThisWorkbook.Sheets("DumpData").Range("A1:AD1")
    Dim frow As Long
    frow = ThisWorkbook.Sheets("DumpData").Range("O" & Rows.count).End(xlUp).Row
    Set rng = ThisWorkbook.Sheets("DumpData").Range("O2:O" & frow)
    Dim count As Long
    count = 2
    For Each cel In rng
        If cel.Value = "Desired" Then
           Set PTRange = Union(PTRange, Range("A" & count & ":AD" & count))
           count = count + 1
        End If
    Next cel

But an error message pops up "Method Union of Object _Global failed" (while setting PTRange in the For Each...Next) loop.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Welcome to the forum.

You're not qualifying the range with a worksheet in that loop:
Rich (BB code):
Set PTRange = ThisWorkbook.Sheets("DumpData").Range("A1:AD1")
    Dim frow As Long
    frow = ThisWorkbook.Sheets("DumpData").Range("O" & Rows.count).End(xlUp).Row
    Set rng = ThisWorkbook.Sheets("DumpData").Range("O2:O" & frow)
    Dim count As Long
    count = 2
    For Each cel In rng
        If cel.Value = "Desired" Then
           Set PTRange = Union(PTRange, ThisWorkbook.Sheets("DumpData").Range("A" & count & ":AD" & count))
           count = count + 1
        End If
    Next cel
 
Upvote 0
Welcome to the forum.

You're not qualifying the range with a worksheet in that loop:
Rich (BB code):
Set PTRange = ThisWorkbook.Sheets("DumpData").Range("A1:AD1")
    Dim frow As Long
    frow = ThisWorkbook.Sheets("DumpData").Range("O" & Rows.count).End(xlUp).Row
    Set rng = ThisWorkbook.Sheets("DumpData").Range("O2:O" & frow)
    Dim count As Long
    count = 2
    For Each cel In rng
        If cel.Value = "Desired" Then
           Set PTRange = Union(PTRange, ThisWorkbook.Sheets("DumpData").Range("A" & count & ":AD" & count))
           count = count + 1
        End If
    Next cel

Thanks, it worked.
 
Upvote 0

Forum statistics

Threads
1,215,931
Messages
6,127,759
Members
449,404
Latest member
cburket

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