VBA:如何描述特定string的行

您好我试图创build一个循环,标识列“B”中的特定string,并SUM在同一行的列“D”中的值。 到目前为止,我已经能够识别“现金”,但是现在我不知道如何描述同一行的列“D”并且总结它。 请帮忙!

下面是我到目前为止的这个循环。

Dim CD As Long Dim text As String Dim Z As Long CD = 0 For Z = 1 To Range("B" & Rows.Count).End(xlUp).Row text = Range("B" & Z).Value If Left(text, 4) = "Cash" Then Sum.... 

按照你提供的代码,这将是你在找什么:

 Sub calculateSum() Dim CD As Long Dim text As String Dim Z As Long 'Create a variable to store the sum and set its value to 0. Dim sum As Double sum = 0 CD = 0 For Z = 1 To Range("B" & Rows.Count).End(xlUp).Row text = Range("B" & Z).Value If Left(text, 4) = "Cash" Then 'Increase the sum by the value stored in column D on the same row. sum = sum + Range("D" & Z).Value End If Next Z 'Display the final result (sum). MsgBox "Sum: " & sum End Sub 

你当然可以做这样的事情:

 For Z = 1 To Range("B" & Rows.Count).End(xlUp).Row text = cells(Z,2).Value If Left(text, 4) = "Cash" Then Sum.... Zum = Zum + cells(Z,4).value 

但是,计算可以用一个简单的工作表公式来完成

 =SUMIF(B:B,"cash*",D:D)