+ Reply to Thread
Results 1 to 15 of 15

Comparing input value in a cell to a range of data

Hybrid View

hoongz Comparing input value in a... 06-25-2012, 05:28 AM
tom1977 Re: Comparing input value in... 06-25-2012, 05:34 AM
abousetta Re: Comparing input value in... 06-25-2012, 05:35 AM
hoongz Re: Comparing input value in... 06-25-2012, 06:13 AM
mmmarks Re: Comparing input value in... 06-25-2012, 07:21 AM
hoongz Re: Comparing input value in... 06-25-2012, 10:10 PM
abousetta Re: Comparing input value in... 06-25-2012, 01:43 PM
hoongz Re: Comparing input value in... 06-25-2012, 10:03 PM
hoongz Re: Comparing input value in... 06-26-2012, 02:51 AM
abousetta Re: Comparing input value in... 06-26-2012, 05:00 AM
hoongz Re: Comparing input value in... 06-26-2012, 11:07 PM
hoongz Re: Comparing input value in... 06-27-2012, 04:55 AM
hoongz Re: Comparing input value in... 06-29-2012, 02:16 AM
hoongz Re: Comparing input value in... 06-29-2012, 06:16 AM
abousetta Re: Comparing input value in... 06-29-2012, 08:03 PM
  1. #1
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Comparing input value in a cell to a range of data

    Hi. Im wondering if it is possible to compare a value in a cell to a range of pre defined values. For example the user input a value "apple" in cell A1. Under column B1:B6 are 6 different values. If the value in A1 matches any of the value in B1:B6, it will change the cell A1 to a certain color, and to another color if the value doesn't match any of the value in B1:B6. Is there any way to do it in VBA?

    Thank you

  2. #2
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Comparing input value in a cell to a range of data

    Hi check such event code
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address(0, 0) = "A1" Then
    If Application.WorksheetFunction.Lookup(Target, Range("b1:b6")) = Target Then
    Target.Interior.ColorIndex = 4
    Else:
    Target.Interior.ColorIndex = 3
    End If
    End If
    End Sub
    paste it into module of worksheet
    Last edited by tom1977; 06-25-2012 at 05:43 AM.
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Comparing input value in a cell to a range of data

    You can use conditional formatting. Assuming your input is in A1 and your list is B1:B6 then you can use this formula:

    Formula: copy to clipboard
    =VLOOKUP(A1,B1:B6,1,0)
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  4. #4
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Thanks it works. But If i need to make it more complicated, is it still possible. For example, the user needs to choose a category before typing in the value. Each category has its own range of value. E.g, Meat category has the value of chicken, fish, pork, beef. Veg category has value of carrot, potato, spinich under column C. Fruit category has apple, banana, coconut under column D. The user has to first choose a category and then input the value in A1. Is it possible to check the range of possible values based on a category that had been chosen?

  5. #5
    Forum Contributor
    Join Date
    11-26-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    159

    Re: Comparing input value in a cell to a range of data

    Hi,
    I have used helper column and conditional format. Im not sure whether it will reach your expections .
    Look at atttachemnt
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Quote Originally Posted by mmmarks View Post
    Hi,
    I have used helper column and conditional format. Im not sure whether it will reach your expections .
    Look at atttachemnt
    mmmarks, could you teach me how you make the cell change color as i couldn't see any formula or anything in vba.

    thank you

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Comparing input value in a cell to a range of data

    If you know what the options you want to give people then you can create two drop-down lists and link them. In your situation, if the person chooses "Chicken" then the dropdown menu in A1 would only have the options you want to show them. Data validation at the time of data input is much easier than correcting mistakes later on.

    Hope this helps.

    abousetta

  8. #8
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Actually the problem is that right now I do know what are the values to provide, but each catrgory have a different number of values to choose from. And I actually need to check against a number of cells because there might be multiple inputs. Im just testing with one input right now by what I aim to acomplish is a sheet where people input multiple data and it is checked against the given value. But the given value might change over time and also the number of values in each category. To give a better understanding, what i need to do is maybe in column A a name input, column B is the input value. There could be multiple records but the good thing is it will only be records for one category. So for example the user have chosen meat, and input 3 records of 3 names and under the input type chicken chicken and potato. But the number of records the user entered each time is different and my set of values might change once in a while when the stock is out or new product is offered. Not sure if I'm confussing people. Sorry if I'm asking for too much.

  9. #9
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Any Advise?

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Comparing input value in a cell to a range of data

    Can you create a sample workbook with dummy data so we can see how you imagine the final product will look? As for the example by @mmmarks, they are using conditional formatting to change the color if the contents of A1 is the same as B2. The formula is hidden within the conditional formatting function and that's why you can't see it. Click on the Conditional formatting icon on the home tab while cell A2 is selected and you should be able to see it.

  11. #11
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Attached is a sample file. Each time the file should only handle one category but should accept multiple value that falls into that category. As the number of input is not fixed, and the range of each category wmight change over time. Im not sure how can I do a proper validation. Thanks in advance for all assistance

    sampling.xlsm

  12. #12
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Anybody understand me or need me to change what im trying to display?

  13. #13
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Anyone able to offer assistance?

  14. #14
    Registered User
    Join Date
    09-20-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Comparing input value in a cell to a range of data

    Anyone please

  15. #15
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Comparing input value in a cell to a range of data

    Hi,

    Sorry, been busy. I have looked at your workbook and think you have two options:

    1) Instead of using drop-down menus, use data validation instead.

    2) If you want to keep the drop-down menus then you can use vba to populate the drop-downs.

    http://www.contextures.com/xlDataVal01.html contains a detailed approach to both options. Let me know if you need more help. (the end of the aforementioned webpage has a list of possible scenarios, each with its own set of instructions).

    Hope this helps.

    abousetta

+ 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