gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:08:21 25 4
gpt4 key购买 nike

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

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com