+ Reply to Thread
Results 1 to 7 of 7

Finding the sum of numbers in VBA

  1. #1
    Registered User
    Join Date
    10-05-2010
    Location
    London
    MS-Off Ver
    2007/2010/2011(Mac)
    Posts
    12

    Finding the sum of numbers in VBA

    Silly question,

    I have the following for loop:

    Dim i As Integer, count As Integer

    count = 0

    For i = 1 To 10
    count = count + 1
    Next i
    Now im not sure if i need the loop or not but how would i calculate the sum of the numbers that the loop passes i.e. 1,2,3,4,5 etc.
    Last edited by csr_cafc; 10-21-2010 at 09:58 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Finding the sum of numbers in VBA

    There are a few ways:

    Please Login or Register  to view this content.
    Generally speaking it's better to use Longs than Integers for reasons not worth going into here (else use Byte where valid)

  3. #3
    Registered User
    Join Date
    10-05-2010
    Location
    London
    MS-Off Ver
    2007/2010/2011(Mac)
    Posts
    12

    Re: Finding the sum of numbers in VBA

    Sorry should have explained properly; if i say wanted the user to input the values (from 1 to n as opposed to 1 to 10). This needs to be done purely in vba and not invlolving the spreadsheet part at this stage.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Finding the sum of numbers in VBA

    Modify

    Please Login or Register  to view this content.
    the above does not write to the worksheet in any way - it's a simple means of calculating what you want without need for iteration.

  5. #5
    Registered User
    Join Date
    10-05-2010
    Location
    London
    MS-Off Ver
    2007/2010/2011(Mac)
    Posts
    12

    Re: Finding the sum of numbers in VBA

    Ok so i have entered that into my code as follows:

    Please Login or Register  to view this content.
    Ok this seems to work but am i right in assuming that it does not use the for loop?

    If so how can you get the same result but using the for loop which i think was the aim of the exercise that i was given.

    Thanks for all your help so far.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Finding the sum of numbers in VBA

    Your running count should increment by i rather than 1.

    (and presumably you should be To nVariable rather than 10 ... you should use Application.InputBox)

  7. #7
    Registered User
    Join Date
    10-05-2010
    Location
    London
    MS-Off Ver
    2007/2010/2011(Mac)
    Posts
    12

    Re: Finding the sum of numbers in VBA

    Got it working- Many Thanks for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1