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
AI Thread Summary
To create a module-level function in Excel that calculates the sum of integers from 1 to a specified number in a cell, the function needs to be adjusted to dynamically set the loop's upper limit based on the value in that cell. The current function attempts to sum values from the first column for 1000 rows but requires modification. The loop should iterate from 1 to the value in the designated cell (in this case, 10). The corrected loop should look like this: For i = 1 to Cells(row, column of the cell where 10 is located). Inside the loop, the sum should be updated by adding the current value of i. This will ensure that the function accurately computes the total sum of integers up to the specified number.
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
1
Views
2K
Replies
2
Views
4K
Replies
23
Views
2K
Replies
8
Views
4K
Replies
2
Views
2K
Replies
10
Views
2K
Back
Top