Using intersect to check cell address

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Using intersect to check cell address

Post by YasserKhalil »

Hello everyone

I have some merged cells in column C and I am using the code like that

Code: Select all

Sub Merge_cells()
    Dim c As Range
    For Each c In Range("C7:C61")
        If Intersect(Range("C:C"), c.MergeArea) Then
        Debug.Print c.MergeArea.Address
        End If
    Next c
End Sub
This code doesn't work and arises error ... How can I check that the c.MergeArea.Address is in column C only not in other columns?

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Using intersect to check cell address

Post by rory »

You could do something like this:

Code: Select all

    For Each c In Range("C7:C61")
      If c.MergeCells Then
        If Intersect(Range("C:C"), c.MergeArea).Address <> c.MergeArea.Address Then
        Debug.Print c.MergeArea.Address
        End If
      End If
    Next c
but note you'll get repeated output for any merged range with multiple column C cells and cells outside column C.
Regards,
Rory

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Using intersect to check cell address

Post by YasserKhalil »

Thank you very much, Mr. Rory.