'after subtoals need to insert line below them and need to add a top border in excel vba
Dim iRange As Range
Dim iCells As Range
Set iRange = ThisWorkbook.ActiveSheet.UsedRange
For Each iCells In iRange
'If Not IsEmpty(iCells) Then
If iCells.SpecialCells(xlFormulas) = True Then
iCells.BorderAround _
LineStyle:=xlContinuous, _
Weight:=xlThin
End If
Next iCells
Solution 1:[1]
Please, try using the next code. No iteration needed:
Sub BordrsOnSubtotals()
Dim sh As Worksheet, rngForm As Range
Set sh = ActiveSheet
On Error Resume Next 'for the case of no formulas in the sheet...
Set rngForm = sh.UsedRange.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not rngForm Is Nothing Then
rngForm.Offset(1).EntireRow.Insert xlShiftDown
Intersect(rngForm.EntireRow, sh.UsedRange.EntireColumn).Borders(xlEdgeTop).Weight = 3
End If
'Excel inserts lines for the range Areas and the last two are in the same area and needs correction:
sh.Range("A" & sh.rows.count).End(xlUp).Offset(-1).EntireRow.Delete
Intersect(sh.Range("A" & sh.rows.count).End(xlUp).EntireRow, _
sh.UsedRange.EntireColumn).Borders(xlEdgeTop).Weight = 3
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
