简体   繁体   English

仅在Excel中为最后一列到最后一行和最后一行到最后一列为单元格着色

[英]Coloring cells in Excel only of Last Column till Last Row and Last Row till Last Column

I would like to highlight the entire row of the last cell till its last column and vice-versa for the entire column 我想突出显示最后一个单元格的整个行,直到最后一列,反之亦然

I've used EntireRow and EntireColumn method and found that it is bit flawed for my approach as it colors whole row and column. 我使用了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

Give this a try: 试试看:

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)

I would advise to declare and use a worksheet variable (at least), in case you want to call this sub from a different sheet and/or a different workbook. 如果您要从其他工作表和/或其他工作簿中调用此子项,我建议(至少)声明并使用一个工作表变量。

Having said that, this is how your sub would look afterwards: 话虽如此,这就是您的潜艇随后的样子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM