自動フィルタリングされたリストがあります。
私の目標は、表示されている行全体の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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加