gpt4 book ai didi

excel - 基于单元格颜色逐行计算

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

此代码根据单元格颜色“绿色”进行计算。不幸的是,当它到达下一行时,例如“E”行(如图所示)计算不是单独进行的,例如仅适用于 C 行,但它采用 C 行中的值,如图所示。我怎样才能以这样的方式重写代码,使得计算只针对行进行?

enter image description here

Sub Schaltfläche1_Klicken()
Dim wb As Workbook, wq As Object
Dim ws As Worksheet, datDatum
Dim cell As Range
Dim c As Long, r As Long, rng As Range

With Worksheets("Tabelle1")

For c = 3 To 5
For r = 1 To 5
If .Cells(r, c).DisplayFormat.Interior.Color = vbRed Then
If rng Is Nothing Then
Set rng = .Cells(r, c)
Else
Set rng = Union(rng, .Cells(r, c))
End If
End If
Next r

If Not rng Is Nothing Then _
.Cells(8, c).Formula = "=average(" & rng.Address(0, 0) & ")"
Next c
End With
End Sub

最佳答案

不太确定,如果我理解正确的话,但我的理解是:
计算单行中具有标准的单元格的平均值。因此,您在第 1 行有一个平均值,在第 2 行有一个平均值......

这将是我的方法(快速了解您的方法):

Sub Schaltfläche1_Klicken()
Dim wb As Workbook, wq As Object
Dim ws As Worksheet, datDatum
Dim cell As Range
Dim c As Long, r As Long, rng As Range

With Worksheets("Sheet1")

For c = 3 To 5
For r = 1 To 5
If .Cells(r, c).DisplayFormat.Interior.Color = vbRed Then
If rng Is Nothing Then
Set rng = .Cells(r, c)
Else
Set rng = Union(rng, .Cells(r, c))
End If
End If
If Not rng Is Nothing Then _
.Cells(8, c).formula = "=average(" & rng.Address(0, 0) & ")"

Next r
Set rng = Nothing

Next c
End With
End Sub

关于excel - 基于单元格颜色逐行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52269440/

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