Trying to notate ranges of merged cells on a worksheet

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
305
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I'm trying to get a listing of all of the cells that are merged on a worksheet. I want them listed in Column D of 'Sheet1', but I can't get the code quite right. Here's what I have so far:

Code:
Dim c As Range

ThisWorkbook.Sheets("Sheet1").Activate
ThisWorkbook.Sheets("Sheet1").Range("D1").Select

For Each c In ThisWorkbook.Sheets("MySheet").UsedRange

    If c.MergeCells Then
        
        ActiveCell.Value = c.Address
        
        ActiveCell.Offset(1, 0).Select
        
    End If
    
Next

Let's assume the first merged cell on "MySheet" is A1:F1.

What it's doing now is writing "$A$1", then going to the next cell down and writing "$B$1", then down one more and writing "$C$1", and so on.

What I need it to do is write the range that is merged, such as "$A$1:$F$1". I can't figure out how to get it to do it that way. Any ideas?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I finally figured it out. Here's the code, in case it's helpful to anyone else.

Code:
Dim c As Range
Dim RngString As String
Dim CellString As String
Dim i As Integer

i = 2

ThisWorkbook.Sheets("MySheet").Activate
    
For Each c In ThisWorkbook.Sheets("MySheet").UsedRange

    If c.MergeCells Then
    
        CellString = c.Address
        
        ThisWorkbook.Sheets("MySheet").Range(CellString).Activate
        
        RngString = Selection.Address
        
        If RngString <> ThisWorkbook.Sheets("Sheet1").Range("D" & i - 1).Value Then
            ThisWorkbook.Sheets("Sheet1").Range("D" & i).Value = RngString
            i = i + 1
        End If
        
    End If
    
Next
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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