簡體   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