[英]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.