VBA Example - Calculate Sum Of Cells
This example tells you how to calculate sum of values using VBA. Of course, you can achieve this by using the SUM() function without any VBA. This example demonstrates how to achieve it using VBA.
Suppose you have a sales data for four regions, and you want to know the total of sales. See the data below.
The VBA code is this:
Sub sum_sales()
Dim row As Integer
Dim sum As Single
sum = 0
Dim sale As Single
For row = 2 To 5
sale = Cells(row, 2) 'grab cell values
sum = sum + sale
Next
Cells(6, 2) = sum 'put the calculated result in the right cell
MsgBox ("done")
End Sub
By running the code, you get the sum:
Comments
Post a Comment