如何在特定单元格上设置边框

时间:2018-08-10 13:34:15

标签: excel vba border

我有一个Excel文件,该文件按文章,ID和启动日期排序,这意味着我在同一篇文章中有几行。

我想在具有相同文章的所有行下放置底部边框。

我有这个,但是它只在标题行下放置边框。

Dim count As Integer
count = 0
For Each x In Range("G1").End(xlDown)
    count = count + 1
    Range(Cells(count, 1), Cells(count, 16)).Select
    With Selection.Borders(xlBottom)
     .LineStyle = xlContinuous
     .Color = vbBlack
     .Weight = xlThick
    End With
Next x

1 个答案:

答案 0 :(得分:1)

如果您希望突出显示数据的最底部,请用ws.Range("G" & ws.Rows.Count).End(xlUp).Offset(1).Row

将循环的上限偏移1
Sub BorderPatrol()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim i As Long

For i = 2 To ws.Range("G" & ws.Rows.Count).End(xlUp).Row
    If ws.Range("G" & i) <> ws.Range("G" & i - 1) Then
        With ws.Range("G" & i - 1).EntireRow.Borders(xlEdgeBottom) 
            .Color = vbBlack
            .Weight = xlThick
        End With
    End If
Next i

End Sub