简体   繁体   English

根据同一行中另一个单元格的值计算相应单元格中的单元格值

[英]Count cell values in corresponding cell based on the value of another cell in the same row

I have a program where it counts the number of cells based on a value.我有一个程序,它根据一个值计算单元格的数量。

For example, if a cell value in column E = "75", add to the "Air Count", else if column E = "76", add to the "Road Count"例如,如果 E 列中的单元格值 =“75”,则添加到“Air Count”,否则如果列 E =“76”,则添加到“Road Count”

In my data set there is a corresponding cell in column S, with the number of items.在我的数据集中,S 列中有一个相应的单元格,其中包含项目数。 I would like to count those items based on their values in Column E. For example, if the cell in column E = "75", then check the corresponding cell in column S, and add it to an "Air Item Count".我想根据它们在 E 列中的值来计算这些项目。例如,如果 E 列中的单元格 =“75”,则检查 S 列中的相应单元格,并将其添加到“Air Item Count”中。

I think I'm a bit confused as to where to add this in. I have attempted to add it in, but I haven't got it correct so I just commented it out.我想我对在哪里添加它有点困惑。我试图添加它,但我没有正确,所以我只是把它注释掉了。

Here is my code:这是我的代码:

Sub ParseData()

Dim airConCount As Integer, airItemCount As Integer, roadConCount As Integer, roadItemCount As Integer, totalConCount As Integer, _
totalItemCount As Integer

'Find last value in row
Dim LastSVS As Integer
Dim LastRowITEMS As Integer


LastRowSVS = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).Row
'LastRowITEMS = ActiveSheet.Cells(Rows.Count, 19).End(xlUp).Row

Dim cel As Range
Dim cel2 As Range
Dim airCounter As Integer
Dim roadCounter As Integer

airCounter = airCounter + 1
roadCounter = roadCounter + 1

'CONSIGNMENT NOTE COUNT
    For Each cel In Range("E2:E" & LastRowSVS)

        If cel.Value = "75" Or cel.Value = "48N" Or cel.Value = "15N" Or cel.Value = "29" Or cel.Value = "701" _
        Or cel.Value = "15D" Or cel.Value = "EP3" Or cel.Value = "X12" Or cel.Value = "753" Or cel.Value = "EP5" _
        Or cel.Value = "X12" Or cel.Value = "753" Or cel.Value = "EP5" Or cel.Value = "1" Or cel.Value = "4" _
        Or cel.Value = "INT" Or cel.Value = "17B" Or cel.Value = "73" Then

        airCounter = airCounter + 1

        Set cel2 = Range("S2:S" & airCounter)


        airConCount = airConCount + 1
        'airItemCount = airItemCount + cel2.Item(19)



        ElseIf cel.Value = "76" Then

        roadConCount = roadConCount + 1
        Set cel2 = Range("J2:J" & roadCounter)
        'roadItemCount = roadItemCount + cel2.Item(19)


        End If

    Next cel

Here is a screenshot of my dataset.这是我的数据集的屏幕截图。 数据集

I would like the Items counted, if they match an attribute in Service.如果它们与服务中的属性匹配,我想要计数的项目。 So, if the Service is 75, count all items in the corresponding row.因此,如果 Service 为 75,则计算相应行中的所有项目。

I think this does what you need it to do.我认为这可以满足您的需要。

It loops through all the cells in the E2:E range and depending on if the value is 76 it adds the value of the cell in column S of the same row to roadItemCount .它遍历E2:E范围内的所有单元格,并根据值是否为 76 将同一行的 S 列中的单元格的值添加到roadItemCount If the value is anything else, it does the same logic but adds the value to airItemCount .如果该值是其他任何值,它会执行相同的逻辑,但会将值添加到airItemCount

I've inversed your if/else logic so if you really need to select those specific values in column E you can use your logic.我已经反转了你的 if/else 逻辑,所以如果你真的需要 select E 列中的那些特定值,你可以使用你的逻辑。

Sub ParseData()

Dim airConCount As Integer, airItemCount As Integer, roadConCount As Integer, roadItemCount As Integer, totalConCount As Integer, _
totalItemCount As Integer

'Find last value in row
Dim LastSVS As Integer
Dim LastRowITEMS As Integer


LastRowSVS = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).Row
LastRowITEMS = ActiveSheet.Cells(Rows.Count, 19).End(xlUp).Row

Dim cel As Range
Dim airCounter As Integer
Dim roadCounter As Integer

airCounter = airCounter + 1
roadCounter = roadCounter + 1

'CONSIGNMENT NOTE COUNT
For Each cel In Range("E2:E" & LastRowSVS)

    If cel.Value = "76" Then
        roadConCount = roadConCount + 1
        roadItemCount = roadItemCount + ActiveSheet.Cells(cel.Row, 19).Value

        'MsgBox ("roadConCount: " & roadConCount)
        'MsgBox ("roadItemCount: " & roadItemCount)
    Else
        airConCount = airConCount + 1
        airItemCount = airItemCount + ActiveSheet.Cells(cel.Row, 19).Value

        'MsgBox ("airConCount: " & airConCount)
        'MsgBox ("airItemCount: " & airItemCount)
    End If

Next cel

MsgBox ("airItemCount: " & airItemCount)
MsgBox ("roadItemCount: " & roadItemCount)

End Sub

暂无
暂无

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

相关问题 根据同一行中另一个单元格的文本颜色设置一个单元格的值 - Set value of a cell based on the text color of another cell on the same row 用于根据同一行中的另一个单元格值过滤行中的单元格的 VBA - VBA for filtering cells in a row based on another cell value in the same row 在列中查找单元格值(多个实例),然后将相应的行值复制到另一行(针对其他单元格值) - Look for a cell value (more than one instance) in a column then copy corresponding row values to another row (against other cell value) 根据标题引用,根据同一行中另一个单元格的值清除单元格的内容 - Clearing contents of a cell based on the value of another cell within the same row, based on header references Excel:如何根据同一行中的另一个单元格锁定一个单元格 - Excel: How to lock a cell based on another cell in the same row 获取基于同一行中另一个单元格的单元格内容? - Get content of cell based on another cell in the same row? 根据同一行中另一个单元格中的数据格式化单元格 - Formatting a cell based on data in an another cell in same row 如果工作表上的单元格值与另一张表上的单元格值相同,则删除该行 - Delete the row if the cell value on sheet is the same then the cell value on another sheet 如何基于同一行中的另一个单元格突出显示列中单元格值的所有实例? - How to highlight all instances of a cell value in a column based on another cell in same row? Excel VBA-将单元格值(如果为空)复制到同一行中的另一个单元格 - Excel VBA - Copy Cell Value, if empty, to another Cell in the same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM