Cell Borders in VBA

Enzo_Matrix

Board Regular
Joined
Jan 9, 2018
Messages
113
Code:
    ActiveSheet.Range("A5:I50").Select
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With

My range of data changes as I add or remove information from my table.

Is there a way to modify this or add a few lines so that only the cells that have information in them are underlined?
Code:
IF cell "" Then ""?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Maybe this:

Code:
Sub test()
Dim cell As Range
Dim myRange As Range

Set myRange = Range("A5:I50")

For Each cell In myRange
    If cell.Value <> "" Then
        With cell.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
        End With
    End If
Next cell

End Sub

Selecting is almost never necessary. The recorder loves to Range(xxxxx).Select and then Selection. (do something) on the next line.

Any time you see that, you can combine the 2 lines like Range(xxxxx).(do something)
 
Last edited:
Upvote 0
if you utilise LastRow code then you could

Range("A5:I"& LastRow)
 
Upvote 0
I think that is the best way as sometime there are 10 lines of data and sometimes up to 100.

Could you show my how to incorporate that '& LastRow' function into the initial code I wrote?
 
Upvote 0
Does this do what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub UnderlineDataCells()
  With Range("A5:I50").SpecialCells(xlConstants).Borders(xlBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
That is exactly what I was hoping for.

Is there a way to modify the range so that it automatically stops at the bottom row?
 
Upvote 0
You very kindly provided this code for me a few months back and a bug has started cropping up in my sheets.
For some reason there are blank rows that are underlined and I cannot figure out why.

This is the code I think might be causing the issue.
Code:
Sub FilterCopy()   
   Dim Ary As Variant
   Dim i As Long
   Dim Sht As Variant
     
   Ary = Array("Weld", "Composite", "Rubber", "Repairs")
   For Each Sht In Ary
      Sheets(Sht).UsedRange.ClearContents
   Next Sht
   With Sheets("Data")
      If .AutoFilterMode Then .AutoFilterMode = False
      For i = 0 To UBound(Ary)
         .Range("A4").AutoFilter 1, Ary(i)
         On Error Resume Next
         .UsedRange.Offset(1).SpecialCells(xlVisible).Copy Sheets(Ary(i)).Range("A" & Rows.Count).End(xlUp).Offset(4)
         On Error GoTo 0
      Next i
      .AutoFilterMode = False
   End With
    
End Sub

Here is a portion of my main code where the underlining is segmented in.
Code:
' Color formatting of cells based on date range    With Sheets("Composite")
        For Each cell In Range("G5:G" & LR)
            myrow = cell.Row
            If cell <= Date Then
                Range(Cells(myrow, "A"), Cells(myrow, "G")).Interior.Color = RGB(217, 217, 217)
            End If
        Next cell
    End With
        
'Underlining cells based on date range
    With Range("A5:I50").SpecialCells(xlConstants).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    
'inserting a blank line at the end of each week
For i = LR To 5 Step -1
    If Cells(i, Col).Value >= Date Then
        On Error Resume Next
            If Cells(i, Col).Value - Cells(i - 1, Col).Value > 1 Then
                On Error GoTo 0
                Cells(i, Col).EntireRow.Insert
            End If
    End If
Next i

If you have a chance, please take a look and let me know if there is something I can do to fix the problem.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top