Closed Thread
Results 1 to 17 of 17

Conditional input in a cell based on data in two different cells and other values

  1. #1
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Conditional input in a cell based on data in two different cells and other values

    I have lot of data in three columns like this. The first column is "Name", the second is "Comment" wherein I want the macro to write some comment, the third one is DOB. The problem is that the names in column 1 repeats many times. I want a macro to write in column B "either Old or Older or Oldest" based on the Name and DOB. Thus David with DOB 13 Sep 1982 be marked Oldest in Column B and David with DOB 25 Aug 1988 be marked Older and David with DOB 24 May 1990 be marked Old. Similarly William and Rita should be marked either old or older or oldest. As the data are enormous I do not want to refer the actual name in the programming. I want to call them using a variable in programming.
    Name Comment DOB
    David 25 August 1988
    David 13 September 1982
    David 24 May 1990
    William 24 March 1980
    William 25 July 1987
    William 13 August 1989
    Rita 17 July 1990
    Rita 24 April 1989
    Rita 13 June 1988
    If such a task can be performed using VBA? Any help.
    P.S. The example file is attached
    Attached Files Attached Files

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Conditional input in a cell based on data in two different cells and other values

    hi vivekchha
    Will there always be three names?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Conditional input in a cell based on data in two different cells and other values

    With the assumption that there will be more than three identical items in the first column, try this,

    =SUMPRODUCT(($B$4:$B$17=B4)*(D4>$D$4:$D$17))+1&" "&B4, adjust the ranges to suit.

    Here is your example back
    Attached Files Attached Files
    Last edited by davesexcel; 12-05-2009 at 06:58 AM.

  4. #4
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Conditional input in a cell based on data in two different cells and other values

    Quote Originally Posted by pike View Post
    hi vivekchha
    Will there always be three names?
    Thanks Pike,
    Some time there may be two names and some times there may be four or five names also.

  5. #5
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Conditional input in a cell based on data in two different cells and other values

    Quote Originally Posted by davesexcel View Post
    With the assumption that there will be more than three identical items in the first column, try this,

    =SUMPRODUCT(($B$4:$B$17=B4)*(D4>$D$4:$D$17))+1&" "&B4, adjust the ranges to suit.

    Here is your example back
    Thanks Moderator,
    You have done it but my problem is that I do not want to rank them. I want to put some other comments based on the ranking. That's why I took the example of Old, older or oldest. Any way, I liked the way you have used the SUMPRODUCT function. Will you please throw some light on how the logic is flowing in the above formula.

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Conditional input in a cell based on data in two different cells and other values

    Quote Originally Posted by vivekchha View Post
    Thanks Moderator,
    You have done it but my problem is that I do not want to rank them. I want to put some other comments based on the ranking. That's why I took the example of Old, older or oldest. Any way, I liked the way you have used the SUMPRODUCT function. Will you please throw some light on how the logic is flowing in the above formula.
    I suppose you could use a nested if if there are not a lot of criterias.
    =if(sumproduct(blah,blah)=1,"Old", if(sumproduct(blah,blah)=2,"Older"

    Not tested.

    Or you may have to use a vlookup funtion.

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional input in a cell based on data in two different cells and other values

    Quote Originally Posted by vivekchha
    Will you please throw some light on how the logic is flowing in the above formula.
    For info. on SUMPRODUCT and how it works see the link in my sig. to Bob Phillips' white paper.

    Quote Originally Posted by vivekchha
    I do not want to rank them. I want to put some other comments based on the ranking.
    You should as outlined use a LOOKUP based approach on the SUMPRODUCT output

    Please Login or Register  to view this content.
    You would not want to embed the same SUMPRODUCT function repeatedly within in an IF

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional input in a cell based on data in two different cells and other values

    I should add - sorting source data nearly always allows you to use more efficient approaches.

    If you were to sort your data by Name (ascending) and then DOB (descending) then you could simply use:

    Please Login or Register  to view this content.
    (adjusting COUNTIF range as req. - ie to encapsulate your data)

  9. #9
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Conditional input in a cell based on data in two different cells and other values

    Quote Originally Posted by DonkeyOte View Post
    I should add - sorting source data nearly always allows you to use more efficient approaches.

    If you were to sort your data by Name (ascending) and then DOB (descending) then you could simply use:

    Please Login or Register  to view this content.
    (adjusting COUNTIF range as req. - ie to encapsulate your data)
    Thanks brother,
    Thanks brother,
    Your method is working fine. But the method is not working if the name occurs only once in column B or it occurs twice in column B. I would like to mark only "Old", if a name occur only one time and I would like to mark only "Old" and "Older" if the name occur two times and I would like to mark "Old", "Older","Oldest" and "Oldest2" if the name occur 4 times. Similarly "Oldest3" for the fifth time and "Oldest4" for the sixth time. Six is the maximum time a name can repeat in column B.
    At prsent the method suggested by you mark "Oldest" if a name occurs only one time and marks "Older" and "Oldest" if the name occur two times.
    I have got a lot of insight from your suggestion. P.S. Thanks for the recommended readings.
    Thanks again.
    Last edited by vivekchha; 12-06-2009 at 11:48 PM. Reason: Experimenting with the suggestion

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional input in a cell based on data in two different cells and other values

    Quote Originally Posted by vivekchha
    if the name occurs only once in column B or it occurs twice in column B. I would like to mark only "Old", if a name occur only one time and I would like to mark only "Old" and "Older" if the name occur two times and I would like to mark "Old", "Older","Oldest" and "Oldest2" if the name occur 4 times. Similarly "Oldest3" for the fifth time and "Oldest4" for the sixth time. Six is the maximum time a name can repeat in column B.
    Same principle regards sorted data but rule reversed and range modified to look only at prior and current record(s):

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Conditional input in a cell based on data in two different cells and other values

    Thanks brother,
    Your method is working very nicely. Will you please explain me the logic in this method. I understand the lookup function and also countif but I do not understand the arrays.
    Thanks again.
    Last edited by DonkeyOte; 12-07-2009 at 11:59 AM. Reason: unnec. quote removed

  12. #12
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional input in a cell based on data in two different cells and other values

    The inline arrays { } are an alternative to specifying ranges - useful if the values within are constants (ie do not change).

    If you opted to store the outputs Old, Older, Oldest, Oldest2, Oldest3, Oldest4 in say a range for ex. E1:E6 you could use:

    =INDEX($E$1:$E$6,COUNTIF($B$4:$B4,$B4))

    and copy down

    For more info. on inline array constants and their use see:

    http://office.microsoft.com/en-us/ex...872911033.aspx
    http://www.bettersolutions.com/excel...N516980331.htm
    etc...

    (ie google and you will find!)

  13. #13
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Conditional input in a cell based on data in two different cells and other values

    Thanks brother,
    Any suggestion as to how I can use this method in VBA.
    Last edited by DonkeyOte; 12-08-2009 at 04:28 AM. Reason: unnec. quote

  14. #14
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional input in a cell based on data in two different cells and other values

    You will need to elaborate.

    Please do not quote entire posts in your response - use the Post Reply button or if you opt to Reply via Quote please Quote only those parts required to make sense of your subsequent post. Unnec. quotes merely clutter up the board.

  15. #15
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Conditional input in a cell based on data in two different cells and other values

    Big Brother,
    The following method is working fine in excel after sorting the data in ascending order first using the DOB key and again sorting the data in ascending order using the Name key.
    C4:
    =LOOKUP(COUNTIF(B4:B14,B4),{1,2,3,4,5,6},{"Old","Older","Oldest","Oldest2","Oldest3","Oldest4"})
    I have tried to use it in VBA using the following codes:
    Workbooks("Name").Worksheets("Data").Range("C4").FormulaArray= "=LOOKUP(COUNTIF(B4:B14,B4),{1,2,3,4,5,6},{"Old","Older","Oldest","Oldest2","Oldest3","Oldest4"})"

    But the codes are reddened and does not work. Will you please suggest where is the mistake. I have also tried using RC type of cell references but that too is not working.

  16. #16
    Registered User
    Join Date
    09-15-2010
    Location
    Hull, England
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Conditional input in a cell based on data in two different cells and other values

    I have lots of data in 4 columns. I need to copy to the next column 'E' the latest 'Total holding' for each year for each director's id, if the value is not the latest from a particular year I need '0'.
    So I need E2=12,509.00
    E3=0
    E4=0
    E5=88,282.00
    E6=25,282.00
    and so on...
    Any help, please??
    Attached Files Attached Files

  17. #17
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: Conditional input in a cell based on data in two different cells and other values

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.

Closed 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