How to Write a VBA Function to Calculate the Sum of Numbers in a Cell?

  • Thread starter Thread starter rose_55
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
SUMMARY

The discussion focuses on creating a VBA function to calculate the sum of numbers from 1 to a specified value in a cell, specifically using Excel's VBA environment. The user initially attempted to sum values in the first column for 1000 rows but needed guidance on dynamically setting the loop's upper limit based on the cell's value. The corrected approach involves using the cell's value to determine the loop's range, allowing for accurate summation from 1 to that value.

PREREQUISITES
  • Familiarity with VBA programming in Excel
  • Understanding of Excel cell references
  • Basic knowledge of loops in programming
  • Concept of summation in mathematics
NEXT STEPS
  • Learn how to use Excel's VBA to manipulate cell values
  • Study the use of loops in VBA for dynamic calculations
  • Explore error handling in VBA functions
  • Investigate optimization techniques for VBA performance
USEFUL FOR

This discussion is beneficial for Excel users, VBA programmers, and anyone looking to automate calculations in Excel through custom functions.

rose_55
Messages
1
Reaction score
0
help pleaase . here is the problem :If a number in a cell is 10, then the total should be 55 which is: 1+2+3+4+5+6+7+8+9+10 , how do i write a module-level( function) like that .this is what i have so far:
Public Function myfunction()
Dim sum As Double
Dim i As Integer
For i = 1 To 1000 Step 1
sum = sum + cells(i,1)...( THIS IS THE PART THAT I'M STUCK , I'M NOT GOOD AT MATH )
Next
myfunction= sum
End Function
 
Last edited:
Technology news on Phys.org
Your function sums up the contents of the first column for 1000 rows. You need the upper limit of your loop to be determined by the cell which contains 10.


For i = 1 to Cells( row, column of cell where 10 lives)

sum = sum+i

next i
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K