繁体   English   中英

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

[英]Coloring cells in Excel only of Last Column till Last Row and Last Row till Last 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

试试看:

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

暂无
暂无

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

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