Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate the sum of values in a column with VBA
#1
Example:

Sub SumColumnValues()
    Dim lastRow As Long
    Dim sumResult As Double

    'Find the last row with data in column A
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    'Calculate the sum of values in column A
    sumResult = Application.WorksheetFunction.Sum(Range("A1:A" & lastRow))

    'Display the sum in a message box
    MsgBox "The sum of values in column A is: " & sumResult
End Sub

Explanation:

The code defines a subroutine named SumColumnValues.
It declares two variables lastRow and sumResult, where lastRow is used to store the last row with data in column A, and sumResult stores the sum of values in column A.
The Cells(Rows.Count, "A").End(xlUp).Row statement finds the last row with data in column A using the End method in combination with the xlUp direction.
The Application.WorksheetFunction.Sum function is used to calculate the sum of the range Range("A1:A" & lastRow), which contains all the values in column A from row 1 to the last row.
The calculated sum is then displayed in a message box using the MsgBox function.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Autofill a series of numbers in a column Qomplainerz 0 255 07-27-2023, 11:23 AM
Last Post: Qomplainerz
  Sum values based on criteria Qomplainerz 0 242 07-27-2023, 11:20 AM
Last Post: Qomplainerz
  Calculate factorial of a given number in VBA Qomplainerz 0 253 07-27-2023, 11:04 AM
Last Post: Qomplainerz
  Calculate the avg and color the cells when their values are above or below the avg Qomplainerz 0 273 07-27-2023, 10:41 AM
Last Post: Qomplainerz
  Copy and paste values with VBA Qomplainerz 0 236 07-27-2023, 09:08 AM
Last Post: Qomplainerz
  Change cell colors based on values with VBA Qomplainerz 0 211 07-27-2023, 08:19 AM
Last Post: Qomplainerz
  Calculate the factorial of a number with VBA Qomplainerz 0 242 07-27-2023, 07:56 AM
Last Post: Qomplainerz
  Calculate the average of three numbers in VBA Qomplainerz 0 222 07-27-2023, 07:40 AM
Last Post: Qomplainerz
  How to format an entire column as number without thousand separator in Excel VBA Qomplainerz 0 356 04-13-2023, 12:26 PM
Last Post: Qomplainerz
  How to format an entire column as number with thousand separator in Excel VBA Qomplainerz 0 329 04-13-2023, 12:25 PM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 2 Guest(s)