gpt4 book ai didi

excel - 对于每列突出显示最大值(Excel)

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

我有一个 Excel 工作表,其中的值来自单元格 2:21 的每一列

我需要突出显示每列中具有最大值的相应单元格,并尝试使用宏循环遍历它。但我只知道如何针对给定的硬编码范围执行此操作..

Private Sub Worksheet_Activate()
Dim zelle As Range
For Each zelle In ActiveSheet.Range("B2:B21")
If zelle.Value = Application.WorksheetFunction.Max(Range("B2:B21")) Then
zelle.Interior.ColorIndex = 6
Else
zelle.Interior.ColorIndex = xlNone
End If
Next
End Sub

我尝试为列使用一个新的范围,我给了该范围(“B:IT”)并迭代该范围,但这不起作用。

也许只有 2 或 3 行?

最佳答案

这可能对你有用。它不使用硬编码范围,而是循环使用任何列并针对具有不同“长度”的列进行调整。它假定只有一个标题行和列。

Private Sub Worksheet_Activate()
Dim zelle As Range
Dim rng As Range
Dim lCol As Long
Dim lLastRow As Long
With ActiveSheet
For lCol = 2 To .UsedRange.Columns.Count
lLastRow = .Cells(.Rows.Count, lCol).End(xlUp).Row
Set rng = .Range(.Cells(2, lCol), .Cells(lLastRow, lCol))
For Each zelle In rng
If zelle.Value = Application.WorksheetFunction.Max(rng) Then
zelle.Interior.ColorIndex = 6
Else
zelle.Interior.ColorIndex = xlNone
End If
Next
Next lCol
End With
End Sub

关于excel - 对于每列突出显示最大值(Excel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8397361/

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