gpt4 book ai didi

excel - 循环过滤列表并突出显示总可见行的 20%

转载 作者:行者123 更新时间:2023-12-04 21:51:03 26 4
gpt4 key购买 nike

我有一个自动过滤的列表。

我的目标是突出显示总可见行的 20%。

我已经有一个代码,但是,我不确定如何从第一个可见行开始突出显示 20%。

下面的代码突出显示所有可见行:

Sub HighlightTwentyPercent()

Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim rng As Range
Set sht = Worksheets("Input raw")
Set StartCell = Range("F2")
Dim cl As Range


LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column

Set rng = sht.Range(StartCell, sht.Cells(LastRow, 6))

For Each cl In rng.SpecialCells(xlCellTypeVisible)

cl.Interior.Color = RGB(255, 12, 29)

Next cl

End Sub

例子:

如果总可见行是 50,那么它将突出显示 20%。在这种情况下,它将突出显示第一个可见行的 10 行。

最佳答案

以下代码将计算等于可见行的 20% 的行数,然后在达到该数字后退出 For 循环:

Sub HighlightTwentyPercent()

Dim sht As Worksheet: Set sht = Worksheets("Input raw")
Dim LastRow As Long, LastColumn As Long
Dim StartCell As Range, rng As Range
Dim cl As Range
Dim TwentyPc As Long, i As Long
Set StartCell = Range("F2")

LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column

Set rng = sht.Range(StartCell, sht.Cells(LastRow, 6))
TwentyPc = Round(rng.Rows.SpecialCells(xlCellTypeVisible).Count * 0.2, 0)

For Each cl In rng.SpecialCells(xlCellTypeVisible)
i = i + 1
If i > TwentyPc Then Exit For
cl.Interior.Color = RGB(255, 12, 29)
Next cl
End Sub

关于excel - 循环过滤列表并突出显示总可见行的 20%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54129117/

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