2017-04-20 128 views
0

在excel中使用VBA,我試圖在列a中的特定文本之後添加一個新行,然後求和列b中的成本。在另一列excel中基於特定值的總和excel VBA

這個新行應該採用指定的文本「蘋果」,並在第a列中添加單詞總數。在b欄中,它應該簡單地總和所有的「蘋果」。

A列有3種水果(蘋果,橘子和香蕉)的隨機數量。

B列有每個蘋果,橘子和香蕉的成本。

它應該是這個樣子

Apple $12  
Apple $12  
Apple $12  
orange $13  
orange $13  
Banana $7  
Banana $7 

Apple $12  
Apple $12  
Apple $12  
Apple Total $36  
orange $13  
orange $13  
orange Total $26  
Banana $7  
Banana $7  
Banana Total $14 
+3

堆棧溢出是不是我的網站的代碼,你有什麼嘗試?請在原帖中發佈您的代碼。 –

回答

0

你可能想改變邊界的循環(我只是做它經過排20)。這也適用於貨幣值。

Dim i As Integer 
Dim j As Integer 
Dim frt As String 

j = 1 

For i = 2 To 20 
    If Cells(i, 1).Value <> Cells(i - 1, 1).Value Then 
     Range("A" & i).EntireRow.Insert 
     frt = Cells(i - 1, 1).Value & " Total" 
     Cells(i, 1).Value = frt 
     Cells(i, 2).Value = Application.WorksheetFunction.Sum(Range("B" & j, "B" & i - 1)) 
     j = i + 1 
     i = i + 1 
    End If 
Next i 

End Sub