我要突出显示最后一个单元格的整个行,直到最后一列为止,反之亦然。
我使用了EntireRow和EntireColumn方法,发现它为整个行和列着色时,对我的方法有点缺陷。
Option Explicit
Sub Range_End_Method()
'Finds the last non-blank cell in a single row or column
Dim lRow As Long
Dim lCol As Long
Dim c As Range
'Find the last non-blank cell in column A(1)
lRow = Cells(Rows.Count, 1).End(xlUp).Row
'Find the last non-blank cell in row 1
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
Cells(lRow, lCol).EntireRow.Interior.Color = RGB(174, 240, 194)
Cells(lRow, lCol).EntireColumn.Interior.Color = RGB(174, 240, 194)
End Sub
答案 0 :(得分:4)
尝试一下:
Range(Cells(lRow, 1), Cells(lRow, lCol)).Interior.Color = RGB(174, 240, 194)
Range(Cells(1, lCol), Cells(lRow, lCol)).Interior.Color = RGB(174, 240, 194)
如果您要从其他工作表和/或其他工作簿中调用此子项,我建议(至少)声明并使用一个工作表变量。
话虽如此,这就是您的潜艇随后的样子:
Option Explicit
Sub Range_End_Method()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("sheet name")
With ws
Dim lRow As Long: lRow = .Cells(Rows.Count, 1).End(xlUp).Row 'Find the last non-blank cell in column A(1)
Dim lCol As Long: lCol = .Cells(1, Columns.Count).End(xlToLeft).Column 'Find the last non-blank cell in row 1
.Range(.Cells(lRow, 1), .Cells(lRow, lCol)).Interior.Color = RGB(174, 240, 194)
.Range(.Cells(1, lCol), .Cells(lRow, lCol)).Interior.Color = RGB(174, 240, 194)
End With
End Sub