+ Reply to Thread
Results 1 to 9 of 9

VBA Loops - Help Needed!

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    VBA Loops - Help Needed!

    Hello. I realized with the problem I have I can not solve with formulas due to a need for a loop.
    Currently I have a database full of food, each records has food ID, food name, group (healthy/unhealthy), and price.
    I have a user enter in an amount of money they have, then I want a random meal to be generated. Right now i have randomized meals appearing which is good, but price has not been considered... I think this is where my loop and VBA skills are needed.
    I layout everything in the workbook below. Thank You your help really is apperciated!
    example2.xlsx

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,080

    Re: VBA Loops - Help Needed!

    Assuming the Food Names are unique (as they indeed are at the moment)

    in H4
    =INDEX(C$2:C$18,MATCH(G4,B$2:B$18,0),1)

    this will get you the price

    Mm, I'm guessing you want to pick a selection of meals within the user's budget that they enter. . .
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Registered User
    Join Date
    10-29-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: VBA Loops - Help Needed!

    @ Special-K. Thank you, I know how to access the price value, but I want the formula I have (the meal generator) to take in account the number inputted by the user
    EX: User enters $3.50
    The randomly generated meal that is < = 3.50
    I feel this should be simple to do but I do not know how to do it. (at this point, press DELETE in an empty cell for the forumal to generate a new food item)
    Thank You!

  4. #4
    Registered User
    Join Date
    04-14-2010
    Location
    NZ
    MS-Off Ver
    2007,2010
    Posts
    86

    Re: VBA Loops - Help Needed!

    If you sort the items in ascending order by their price, you could use something like this:

    =INDEX(B2:B18,RANDBETWEEN(2,MATCH(G1,C2:C18,1)),1)

    G1 being where i put the price

  5. #5
    Registered User
    Join Date
    10-29-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: VBA Loops - Help Needed!

    Unfortunatley I cannot mess around with the layout of the database..

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA Loops - Help Needed!

    what about taxes and tips???
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  7. #7
    Registered User
    Join Date
    10-29-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: VBA Loops - Help Needed!

    my updated takes cares of tax, tip it does not. It is not a requirement needed for this system

  8. #8
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,156

    Re: VBA Loops - Help Needed!

    hi biddum. not sure if you received my PM. seems to have problems lately sending out messages. you could provide me a link to the thread you need help with next time. i'm now just guessing it's this one.

    i placed a validation in G1 that it cannot be below the minimum amount of both meals. i also did a VLOOKUP to find the prices of the random meals. then i did a coding to recalculate until it is lesser or equals to the Input Amount.

    the code is as such:
    Sub RandLoop()
    
    Calculate
    Do Until Cells(4, "H") + Cells(5, "H") <= Cells(1, "G")
        Calculate
    Loop
    
    End Sub
    Attached Files Attached Files

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  9. #9
    Registered User
    Join Date
    10-29-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: VBA Loops - Help Needed!

    yes thank you! This was the final step I was looking for! Thank you so much for your help
    Last edited by biddum; 12-02-2012 at 06:49 PM.

+ 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