JIYIK CN >

Current Location:Home > Learning > PROGRAM > Vba >

Sum function in VBA

Author:JIYIK Last Updated:2025/04/16 Views:

We will show you how to use sum in VBA.

Sum function in VBA

Sum is the most commonly used function in excel. This function is very useful as we can use sum to get the total from the financial table.

We will learn how to use the sum function in VBA, assign the result to a variable, and use the sum function in single or multiple ranges. We can WorksheetFunctioneasily use the sum function using .

Let's take an example where we enter some demo data into an Excel worksheet and get the sum of that data.

Code:

# VBA
Sub sumFunction()
Dim result As Double
result = WorksheetFunction.Sum(Range("A2:A8"))
MsgBox "Total is " & result
End Sub

Output:

The sum in the first example of vba is assigned to a variable

We can easily get the sum of a range using the sum function. If we want to get the result of the sum of two ranges, we can use the same function.

Code:

# VBA
Sub sumFunction()
Dim result As Double
result = WorksheetFunction.Sum(Range("A2:A8"), Range("B2:B8"))
MsgBox "Total is " & result
End Sub

Output:

Sum multiple ranges and assign values ​​to variables in vba second example

We can get the sum of multiple ranges using the same function. We can use more than 2 ranges to get the sum and assign the resulting value to a cell in excel.

Let's take an example where we will assign the value of the result to a cell.

Code:

# VBA
Sub sumFunction()
Dim result As Double
result = WorksheetFunction.Sum(Range("A2:A8"), Range("B2:B8"))
Range("C2").value(result)
End Sub

Output:

Using the sum function in VBA and assigning the value to a cell

Previous: None

Next:Adding a new row in VBA

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Remove duplicates in VBA

Publish Date:2025/04/16 Views:104 Category:Vba

We will explain how to remove duplicates in VBA by example. Remove duplicates in VBA When working with an Excel worksheet that contains a lot of data, it is likely that this data contains some duplicates. Any duplicates must be removed to a

Rounding in VBA

Publish Date:2025/04/16 Views:162 Category:Vba

Round() We will introduce the and functions in VBA through examples RoundUp() . Round() Using the or RoundUp() function in VBA When working with Excel worksheets containing numbers and calculations, we get results that are fractions. Someti

Adding a new row in VBA

Publish Date:2025/04/16 Views:194 Category:Vba

We will show you how to continue the code on the next line in VBA by example. We will also show you how to go to the next line in a message box using different methods in VBA. Adding a new row in VBA There are two different situations in pr

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial