如何在一系列合并和未合并的单元格周围放置边框

How to put borders around a range of merged and non-merged cells

我需要一个宏,它可以根据 A 列中的格式在 A 到 F 的一系列单元格中添加边框。在 A 列中,单元格可以与其下方的多个单元格合并,也可以只是一个单元格。我写了一个 VBA 代码来在 A 列中的非空白单元格周围放置一个边框,但不知道如何将它扩展到其他 5 列(从 B 到 F)。看图可以更好的理解我写的和我需要的。

数据的样子:Data

我的代码做了什么:WhatMyCodeDoes

我想要它做什么:WhatIWantItToDo

我的代码:

Sub Borders()
Dim linestyle As Variant, line   As Variant
Range("A1").Select
Do While ActiveCell.Address <> Range("A65536").End(xlUp).Offset(1, 0).Address
If ActiveCell.Value > 0 Then
 linestyle = Array(xlDiagonalDown, xlDiagonalUp, xlInsideVertical, xlInsideHorizontal)
line = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight)
For I = 0 To UBound(linestyle)
Selection.Borders(linestyle(I)).linestyle = xlNone
    With Selection.Borders(line(I))
        .linestyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    Next I
Else
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub

你可以只更改两行就可以了。

'Range("A1").Select
Range("A1").Resize(1, 6).Select
' ...

'ActiveCell.Offset(1, 0).Select
ActiveCell.Offset(1, 0).Resize(1, 6).Select