excel - 查找 Excel 工作表中最后一条彩色线

标签 excel vba

我使用彩色单元格在 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/

相关文章:

Excel VBA 从超链接列表中提取 Web 数据

arrays - Excel VBA - 填充变体数组会增加内存使用量

excel - 如何更改 Excel 2010 中的默认 VBA 引用

集合的 VBA Join 等效项

excel - VBA 字典引用

vba - 使用 VBA 转换为句子大小写

vba - 如何检测单元格格式的变化?

vba - 将多行文本框值与 Excel 行值进行比较,然后在 textbox2 中输出相同的值

excel - 使用 VBA 解析和扩展具有变量的代数方程

excel - VBA Dir 在名为 ".."或 Documents.xlsx 的目录中提取 Ghost 文件