I have an excel template which will later be filled in from a CSV file, then the CSV data will be copied to Excel in columns “A8:C8” including headers, but the amount of data will always change.
So how do you ensure that every data that has been copied to Excel will continue to border the data?
But why are the empty rows and columns in the border?
Is my code wrong?
What is certain is that later the data will increase downwards, not sideways
Sub HighlightFilledRows()
Dim ws As Worksheet
Dim lastRow As Long
Dim rng As Range
Dim cell As Range
' Specify the worksheet name
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
' Find the last row with data in column A
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
' Loop through each row and check if it's filled
For Each rng In ws.Range("A8:C8" & lastRow)
If WorksheetFunction.CountA(rng.EntireRow) > 0 Then
' Highlight the row if it's filled with a black border
rng.EntireRow.Borders.LineStyle = xlContinuous
rng.EntireRow.Borders.Color = RGB(0, 0, 0) ' Black color
Else
' Remove borders if the row is not filled
rng.EntireRow.Borders.LineStyle = xlNone
End If
Next rng