VBA Insert Shape next to each merged cell

tonyjyoo

Board Regular
Joined
Aug 5, 2016
Messages
167
Hello,

I want to utilize a macro to place left braces next to each merged cell. I already have a macro that goes down a column and merges groups of cells (going from A,B,C,etc):

Code:
Sub MergeCells()


  Dim LastRow As Long, Ar As Range
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  For Each Ar In Range("F15:F" & LastRow).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1).Resize(Ar.Count + 1).Merge
  Next
  
End Sub

I am trying to incorporate this line of coding to automatically place left braces around each merged cell, but it seems to only paste the shape in a static place in my file:

Code:
ActiveSheet.Shapes.AddShape(31, ActiveCell.Left, ActiveCell.Top, 6, 32.25).Select

How can I write my VBA so that after each merge, there follows a brace? For example, group "A" would be merged, then a brace would be pasted; group "B" would be merged, then a brace would be pasted, etc.

Thank you,
Tony
 

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.
***Also, if there is a possibility, I would want the brace to autosize itself according to each merged group of row's height as well.
 
Upvote 0
Hi.
See if this could help.
Code:
Sub MergeCells()
  Dim LastRow As Long, Ar As Range
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  For Each Ar In Range("F15:F" & LastRow).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1).Resize(Ar.Count + 1).Merge
    ActiveSheet.Shapes.AddShape 31, Ar.Offset(-1, -1).Left + 30, _
     Ar.Offset(-1, -1).Top, 6, Ar(1).Offset(-1).Resize(Ar.Count + 1).Height
  Next
End Sub
 
Upvote 0
Hi.
See if this could help.
Code:
Sub MergeCells()
  Dim LastRow As Long, Ar As Range
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  For Each Ar In Range("F15:F" & LastRow).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1).Resize(Ar.Count + 1).Merge
    ActiveSheet.Shapes.AddShape 31, Ar.Offset(-1, -1).Left + 30, _
     Ar.Offset(-1, -1).Top, 6, Ar(1).Offset(-1).Resize(Ar.Count + 1).Height
  Next
End Sub

This works beautifully. Just adjusted the parameters slightly to fit my file. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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