+ Reply to Thread
Results 1 to 4 of 4

sum the values using For-Next Loop

  1. #1
    Registered User
    Join Date
    08-18-2020
    Location
    Kuala Lumpur
    MS-Off Ver
    MS Office 2019
    Posts
    28

    sum the values using For-Next Loop

    I am trying to sum the values of all values being run through the for loop. E.g, Column A has 5 values, A1=1 , A2=2 , A3=3, A4=4 , A5=5. I am trying to sum up all the values when the loop runs . I need it to calculate my project profit and loss , the example illustrated is just a simple one of course for the sake of my understanding

    This is my code that I am stuck at.I know its simple and I am missing something here.



    Sub test()
    Dim a As Long

    Dim i As Long

    a = 0
    For i = 1 To 5
    a = Range("A" & i).Value

    Next i



    End Sub

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: sum the values using For-Next Loop


    As per this forum rules when you post a code you must use the # icon !

    As looping is very useless here according to Excel basics with this single VBA codeline : MsgBox [SUM(A1:A5)]

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,882

    Re: sum the values using For-Next Loop

    @elyas91

    You have marked this post as solved. Is that a typo or have you solved this post.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: sum the values using For-Next Loop

    You could use a function to do the job
    Somewhere put =MySUM(A1:A5)

    Please Login or Register  to view this content.
    - Battle without fear gives no glory - Just try

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Loop through values and sum corresponding values from another sheet
    By rebeccagreene in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2021, 09:44 AM
  2. [SOLVED] Nested For Loop help - pasting values below previous values
    By Buzz1126 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-07-2019, 10:46 AM
  3. [SOLVED] Find and Replace Loop + a Loop to remove Duplicate values
    By lougs7 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-23-2018, 07:43 AM
  4. [SOLVED] Input a range of values into a cell and return the values for each loop
    By cavalieregiuseppe in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-10-2018, 11:31 PM
  5. Loop through values in one column and extract values from another?
    By sarah07282 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-31-2014, 06:01 AM
  6. Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range
    By ExcelQuestFL in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-19-2010, 09:19 AM
  7. Replies: 8
    Last Post: 12-08-2008, 12:54 PM

Tags for this Thread

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