如何格式化合并单元格周围的边框vba

如何格式化合并单元格周围的边框vba,vba,excel,excel-formula,Vba,Excel,Excel Formula,我有一个合并的单元格,如下所示 以下是我在其周围设置边界的代码: Dim c As Range For Each c In testing If c.MergeCells Then c.Interior.ColorIndex = 19 c.Borders.LineStyle = xlContinuous c.Borders.Weight = xlThick c.Borders.Colo

我有一个合并的单元格,如下所示

以下是我在其周围设置边界的代码:

 Dim c As Range

 For Each c In testing  
      If c.MergeCells Then
           c.Interior.ColorIndex = 19
           c.Borders.LineStyle = xlContinuous
           c.Borders.Weight = xlThick
           c.Borders.Color = vbGreen
       End If
   Next

此代码仅在左上角单元格周围创建边框(见图)。如何确保边框放置在整个合并单元格的周围?

您必须使用引用范围的
MergeArea

Dim c As Range

For Each c In testing  
   If c.MergeCells Then
       With c.MergeArea
            .Interior.ColorIndex = 19
            .Borders.LineStyle = xlContinuous
            .Borders.Weight = xlThick
            .Borders.Color = vbGreen
       End With 
   End If
 Next
试一试


嗯,我的答案似乎比公认的更简洁、更快。你的解决方案仍然没有在整个问题上划出界限cell@sukhvir我在回答中写了这个“合并单元格的c范围”,我的意思是,如果使用了c,您必须用其他内容更改合并单元格的范围,例如
D=Range(“B2:D22”)
。我是这样写的,因为我怀疑c是合并单元格的范围。更清楚地说,我总是在把代码作为答案发布之前先尝试一下。上面的代码在我的电脑上运行良好。为什么要使用excel公式标签?当您希望从标题中获得VBA解决方案时?
With c        'Range of the merged cell
    .BorderAround , Weight:=xlMedium
    .Borders.Color = vbGreen
End With