繁体   English   中英

如果大于 0,则返回单元格值

[英]Returning cell value if greater than 0

使用嵌套公式或 VBA,我想为所有个人返回每个评分列中大于 0 的评分。 每列的结果应位于最后填充行的正下方,如下所示: 计算前的电子表格:

     Rating A     Ratings B     Ratings C
Jane    0           -1              0
Rick    1           -2              1
Johnny -1            2              5
James   3            2              3

后:

       Rating A        Ratings B         Ratings C
Jane     0                -1                0
Rick     1                -2                1
Johnny  -1                 2                5 
James    3                 2                3
        Rick            Johnny          Johnny
         1                 2                5
        James            James            James
         3                 2                3
                                          Rick
                                            1

如上所示,Rick 和 James 的评级 A > 0,因此公式输出从评级 A 列的下一个空行开始的结果。 评级 B 和 C 的技术相同。请注意,这是一个更大的电子表格的小得多的豁免。 我无法发布包含数千行和列的完整电子表格。

首先,正如urdearboy建议的那样,我会考虑稍微改变输出格式并使用数据透视表。 但是,如果它不是一个选项,您可以尝试for...next循环。

下面的代码将循环思考您的示例并以您想要的方式收集结果,但我冒昧地将结果放在单独的列中,因为它会更加通用以备将来使用。

Sub Check_rating()

Dim x As Long
Dim ws As Worksheet
Dim counterA As Long
Dim counterB As Long
Dim counterC As Long

Set ws = Worksheets("Sheet1")

    For x = 2 To Range("Table1").Rows.Count + 1
        If ws.Cells(x, "B").Value > 0 Then
            counterA = counterA + 1
            ws.Cells(counterA, "F").Value = ws.Cells(x, "A").Value
            ws.Cells(counterA, "G").Value = ws.Cells(x, "B").Value
        End If
        If ws.Cells(x, "C").Value > 0 Then
            counterB = counterB + 1
            ws.Cells(counterB, "H").Value = ws.Cells(x, "A").Value
            ws.Cells(counterB, "I").Value = ws.Cells(x, "C").Value
        End If
        If ws.Cells(x, "D").Value > 0 Then
            counterC = counterC + 1
            ws.Cells(counterC, "J").Value = ws.Cells(x, "A").Value
            ws.Cells(counterC, "K").Value = ws.Cells(x, "D").Value
        End If
    Next x

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM