简体   繁体   中英

Returning cell value if greater than 0

Using nested formulas or VBA, I want to return ratings that are greater than 0 in each rating column for all individuals. The results for each column should be right below the last filled row as shown below: Spreadsheet before calculations:

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

After:

       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

As seen above, Rick and James have Ratings A >0 so the formula outputs the results beginning in the next empty row on Rating A Column. Same technique for Ratings B and C. Please note that this is a much smaller exempt of a larger spreadsheet. I can't post the full spreadsheet that's thousands of rows and columns.

First of all, as urdearboy suggested I would consider changing output format a bit and using pivot table. However if it not an option, you can try for...next loop.

Code below will loop thought your example and collect results in a way you want, but I took a liberty to put results in separate columns, since it would be much more versatile for future use.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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