Method union of object Global failed

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
13,457
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
I have the following macro which produces the error:
Code:
Sub Test()
    Dim uRng As Range
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Summary" Then
            If Not uRng Is Nothing Then
                Set uRng = Union(uRng, ws.Cells(6, 3))
            Else
                Set uRng = ws.Cells(6, 3)
            End If
        End If
    Next ws
End Sub
After doing much research, I can't seem to find a solution to the problem. Any suggestions would be greatly appreciated.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You can't union a range across different worksheets.
 
Upvote 0
Thank you for your quick response. I'm trying to find duplicate values in the same cell across multiple sheets and then highlight the duplicates, if any. For example, check the values in all the C6 cells in all the sheets and highlight the duplicates. Would you have any suggestions? Perhaps I should have posted this question originally. I can start a new thread if you feel that is appropriate.
 
Upvote 0
You could use a dictionary to store the values as keys and the first cell as the item. Then you test for the existence of each value in the loop. Something like this (untested):

Code:
Sub Test()
    Dim uRng As Range
    Dim ws As Worksheet
    Dim d As Object
    
    Set d = CreateObject("scripting.dictionary")
    For Each ws In Sheets
        If ws.Name <> "Summary" Then
            If d.exists(ws.Cells(6, 3).Value) Then
                d(ws.Cells(6, 3).Value).Interior.Color = vbRed
                ws.Cells(6, 3).Interior.Color = vbRed
            Else
                Set d(ws.Cells(6, 3).Value) = ws.Cells(6, 3)
            End If
        End If
    Next ws
End Sub
 
Upvote 0
Thank you so very much. I will give that a try. :)
 
Upvote 0
Worked like a charm. Thanks again. :)
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

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