Annaïg
Annaïg

Reputation: 23

How do I put borders on specific cells

I have an Excel file that is sorted by article, id and launch date, which means I have several rows with the same article.

I want to put bottom borders under all the rows that have the same article.

I have this but it only puts borders under the header row.

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

Upvotes: 1

Views: 68

Answers (1)

urdearboy
urdearboy

Reputation: 14580

If you want this to highlight the very bottom of your data, offset the upper bound of the loop by 1 with ws.Range("G" & ws.Rows.Count).End(xlUp).Offset(1).Row

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

Upvotes: 1

Related Questions