+ Reply to Thread
Results 1 to 10 of 10

coding to add values until a certain limit

  1. #1
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    coding to add values until a certain limit

    Hi,

    i would like to know what is wrong with my coding. This coding is supposed to let excel list down all the items of which the total cost is within budget and paste these items on sheet2. the budget will be declared at cell E2 by user. the result of my coding gives me only the 1st item in sheet 2 although i set the budget to be 100.


    sheet 1:
    Location " risk value " "cost "
    a 12 12
    b 10 32
    c 7 10
    d 5 15

    Please Login or Register  to view this content.
    what if my cost and risk values are formulas based on other factors in sheet1. what adjustments do i have to make to allow excel to copy only the name, values of cost and risk values into sheet 2?
    Last edited by dan2010; 08-16-2010 at 05:22 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: coding to add values until a certain limit

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: coding to add values until a certain limit

    i have uploaded an example. thanks.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: coding to add values until a certain limit

    please ignore the previous attachment.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: coding to add values until a certain limit

    OK, you've got numerous problems with your code.

    In no particular order:

    1. The syntax of your For Next loop is wrong, it's For i=0 to 1000000, not for i=0 to i=100000 - that is a boolean statement which evalutes to 0, so your loop says For i=0 to 0, which is why it only copies the first row.

    2. You don't need to manually increment i - that's what the loop does.

    3. A for next loop is the wrong tool for the job anyway, you'd be better off manually incrementing i and having the whole lot in a While sum=<budget ... wend loop.

  6. #6
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: coding to add values until a certain limit

    i see. while,wend works well. thanks for ur help btw do u know what code can i use to show the values of "risk value" and "cost" from sheet 1 instead of copying the entire row? i am supposed to show only the location,risk value and cost in sheet 2. if i remove the values of other factors, "risk value" and "cost" will turn to 0.

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: coding to add values until a certain limit

    No problem...

    Please Login or Register  to view this content.
    Try that.

  8. #8
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: coding to add values until a certain limit

    its perfect btw using this coding will result in the total cost of items more than budget. so i came up with this "sheet2.cells(i+2,cellloop).delete" to delete the last item. however, it is not working. do u know what is wrong?

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: coding to add values until a certain limit

    Rather than messing around with deleting rows you'd be better off with:

    Please Login or Register  to view this content.
    That should avoid the problem of going over budget.

  10. #10
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: coding to add values until a certain limit

    all right. thanks alot

+ 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