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

时间:2018-11-06 04:48:48

标签: excel vba excel-vba excel-formula

使用嵌套公式或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的技术相同。请注意,这是较大电子表格的较小豁免。我无法发布包含数千行和数千列的完整电子表格。

1 个答案:

答案 0 :(得分:0)

首先,正如 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