Cost Block Allocation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • if1467
    New Member
    • Feb 2008
    • 25

    Cost Block Allocation

    I am using VB in Excel v.6.3.

    I am trying to write a program that will solve the folowing problem:

    I have a list of random numbers (PurchBlks) and the list is a random length (for practical purposes I will use a list of 5 numbers)

    250
    68
    453
    124
    13

    I also have a variable single number (Saleshrs). For example
    743

    I need to have the program figure out how many of the numbers on the list to use going from top to bottom.

    In the example I would use 250, 68, and 425 of the 453.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by if1467
    I am using VB in Excel v.6.3.

    I am trying to write a program that will solve the folowing problem:

    I have a list of random numbers (PurchBlks) and the list is a random length (for practical purposes I will use a list of 5 numbers)

    250
    68
    453
    124
    13

    I also have a variable single number (Saleshrs). For example
    743

    I need to have the program figure out how many of the numbers on the list to use going from top to bottom.

    In the example I would use 250, 68, and 425 of the 453.
    It seems like a homework to me ^.^

    Anyway, you shouldnt have any trouble using a DO / LOOP :

    lets say PurchBlks is an array of doubles (of one dimension), which first index is zero:
    [CODE=vb]
    dim i as long
    dim Str1 as string
    dim Sum1 as double 'if you dont care about the value of Saleshrs after the result is given, you can skip this variable and just make substractions to Saleshrs.

    do
    sum1=sum1 + PurchBlks(i)
    if sum1> Saleshrs then exit do
    str1 = cstr(PurchBlks( i)) & ", "
    i= i+1
    loop

    sum1=sum1 - PurchBlks(i)
    str1 = str1 & " and " cstr(Saleshrs - sum1) & " from " cstr(PurchBlks( i))
    msgbox str1 ' only to show you the result.[/CODE]

    you can improve a lot this code (it's not even good), but i think it'll give you an idea. (the first thing you must add is an UNTIL, after the loop, because if you get to the last index and you never reach Saleshr, it'll show you an error)

    HTH

    Comment

    • if1467
      New Member
      • Feb 2008
      • 25

      #3
      Originally posted by kadghar
      It seems like a homework to me ^.^

      Anyway, you shouldnt have any trouble using a DO / LOOP :

      lets say PurchBlks is an array of doubles (of one dimension), which first index is zero:
      [CODE=vb]
      dim i as long
      dim Str1 as string
      dim Sum as double 'if you dont care about the value of Saleshrs after the result is given, you can skip this variable and just make substractions to Saleshrs.

      do
      sum1=sum1 + PurchBlks(i)
      if sum > Saleshrs then exit do
      str1 = cstr(PurchBlks( i)) & ", "
      i= i+1
      loop

      sum1=sum1 - PurchBlks(i)
      str1 = str1 & " and " Saleshrs - sum1 & " from " cstr(PurchBlks( i))
      msgbox str1 ' only to show you the result.[/CODE]

      you can improve a lot this code (it's not even good), but i think it'll give you an idea. (the first thing you must add is an UNTIL, after the loop, because if you get to the last index and you never reach Saleshr, it'll show you an error)

      HTH
      Thanks, I'll have to take a look at this on Monday, since it is for my full-time job as an investment accountant which is Mon-Fri 9-5. :)

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by if1467
        Thanks, I'll have to take a look at this on Monday, since it is for my full-time job as an investment accountant which is Mon-Fri 9-5. :)
        My apologies.
        But we're not supposed to answer homeworks, but as i answered it, it's clear that i really didnt think it was so.
        Once again, im sorry, i didnt pretend to be rude.

        Comment

        Working...