excel vba排序时如何移动边框?

How to move the border when sorting in excel vba?

我打算根据第一列对数据库进行排序,但是边界线并没有根据排序后的数据移动。

是否可以使用 vba 代码移动它?

这是一个想法。它不是完整的解决方案,但您可以从它开始并通过删除幻数等构建您自己的解决方案...

Option Explicit

Public Sub SortingBorders()

    Dim rngMyRng    As Range
    Dim rngCell     As Range

    Set rngMyRng = Range("A1:A20")
    Call RemoveAllBorders(rngMyRng)

    For Each rngCell In rngMyRng
        If rngCell <> rngCell.Offset(1, 0) Then
            Range(rngCell.Offset(1, 0), rngCell.Offset(1, 5)).Borders(xlEdgeTop).LineStyle = xlContinuous
        End If
    Next rngCell

End Sub

Public Sub RemoveAllBorders(rngMyRng As Range)

    With rngMyRng
        .Borders(xlDiagonalDown).LineStyle = xlNone
        .Borders(xlDiagonalUp).LineStyle = xlNone
        .Borders(xlEdgeLeft).LineStyle = xlNone
        .Borders(xlEdgeTop).LineStyle = xlNone
        .Borders(xlEdgeBottom).LineStyle = xlNone
        .Borders(xlEdgeRight).LineStyle = xlNone
        .Borders(xlInsideVertical).LineStyle = xlNone
        .Borders(xlInsideHorizontal).LineStyle = xlNone
    End With

End Sub

这就是你得到的:

解决方案围绕第 1 列,仅当条件 rngCell <> rngCell.Offset(1, 0) 为真时才放置边框。