I have been trying to develop a code which highlights the last row of the data but my code highlights the complete last row instead of highlighting A to L
.
How to add A to L
Any help will be appreciated.
Dim lngLastRow As Long
lngLastRow = Cells.Find(What:="*", _
After:=Range("A1"), _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Rows(lngLastRow & ":" & lngLastRow).Interior.Color = RGB(255, 217, 102)
Option Explicit
Sub HighlightLastRowSimple()
Dim lRow As Long
lRow = Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
Columns("A:L").Rows(lRow).Interior.Color = RGB(255, 217, 102)
End Sub
Sub HighlightLastRowSafe()
Dim lCell As Range
Set lCell = Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious)
If lCell Is Nothing Then Exit Sub ' empty worksheet
Columns("A:L").Rows(lCell.Row).Interior.Color = RGB(255, 217, 102)
End Sub
Sub HighlightNLastRows(ByVal n As Long)
Dim lCell As Range
Set lCell = Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious)
If lCell Is Nothing Then Exit Sub ' empty worksheet
Dim lRow As Long: lRow = lCell.Row
If lRow < n Then
MsgBox "You cannot highlight " & n & " last rows.",
vbCritical, "Highlight n Last Rows"
Else
Columns("A:L").Rows(lRow).Offset(1 - n).Resize(n) _
.Interior.Color = RGB(255, 217, 102)
End If
End Sub
Sub Highlight5()
HighlightNLastRows 5
End Sub
A:L
) you could rewrite like the following...Sub HighlightLastRowSimple()
Const Cols As String = "A:L"
With Columns(Cols)
Dim lRow As Long
lRow = .Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
.Rows(lRow).Interior.Color = RGB(255, 217, 102)
End With
End Sub
Sub HighlightLastRowSafe()
Const Cols As String = "A:L"
With Columns(Cols)
Dim lCell As Range
Set lCell = .Find("*", , xlFormulas, , xlByRows, xlPrevious)
If lCell Is Nothing Then Exit Sub
.Rows(lCell.Row).Interior.Color = RGB(255, 217, 102)
End With
End Sub
Sub HighlightNLastRows(ByVal n As Long)
Const Cols As String = "A:L"
With Columns(Cols)
Dim lCell As Range
Set lCell = .Find("*", , xlFormulas, , xlByRows, xlPrevious)
If lCell Is Nothing Then Exit Sub
Dim lRow As Long: lRow = lCell.Row
If lRow < n Then
MsgBox "You cannot highlight " & n & " last rows.", _
vbCritical, "Highlight n Last Rows"
Else
.Rows(lRow).Offset(1 - n).Resize(n) _
.Interior.Color = RGB(255, 217, 102)
End If
End With
End Sub
... when possible data to the right wouldn't be considered.