我使用彩色单元格在 Excel 工作表中使用全彩色线条进行分区。
我正在寻找一种方法来轻松获取背景上完全着色的 las 水平线的地址。
我寻找的方式是使用Find方法,但我做不到
With Application.FindFormat
.Clear
.Interior.ColorIndex = xlNone
End With
cell = .Find(What:="", After:=.Cells(.Cells.Count), SearchFormat:=True)
感谢您的帮助
最佳答案
最后一个全彩色行地址
Option Explicit
Sub GetLastColoredRowAddressTEST()
Debug.Print GetLastColoredRowAddress(Range("A1:M20"))
' Caution! When using for whole worksheet use this:
Debug.Print GetLastColoredRowAddress(Sheet1.UsedRange.EntireRow)
End Sub
Function GetLastColoredRowAddress( _
ByVal rg As Range) _
As String
With Application.FindFormat
.Clear
.Interior.ColorIndex = xlNone
End With
Dim rCount As Long: rCount = rg.Rows.Count
Dim rCell As Range
Dim r As Long
For r = rCount To 1 Step -1
Set rCell = rg.Rows(r).Find(What:="", _
SearchDirection:=xlPrevious, SearchFormat:=True)
If rCell Is Nothing Then
GetLastColoredRowAddress = rg.Rows(r).Address
Exit For
End If
Next r
Application.FindFormat.Clear
End Function
编辑
- 这是工作表版本。当然,您应该根据需要重命名这些函数。
Sub GetLastWorksheetColoredRowAddressTEST()
Debug.Print GetLastWorksheetColoredRowAddress(Sheet1)
End Sub
Function GetLastWorksheetColoredRowAddress( _
ByVal ws As Worksheet) _
As String
With Application.FindFormat
.Clear
.Interior.ColorIndex = xlNone
End With
With ws.UsedRange.EntireRow
Dim rCell As Range
Dim r As Long
For r = .Rows.Count To 1 Step -1
Set rCell = .Rows(r).Find(What:="", _
SearchDirection:=xlPrevious, SearchFormat:=True)
If rCell Is Nothing Then
GetLastWorksheetColoredRowAddress = .Rows(r).Address
Exit For
End If
Next r
End With
Application.FindFormat.Clear
End Function
关于excel - 查找 Excel 工作表中最后一条彩色线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71360181/