+ Reply to Thread
Results 1 to 11 of 11

Loop to assign values based on condition

  1. #1
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Loop to assign values based on condition

    Hi,

    I am trying to loop through two columns ( Col1,Col2) and based on a condition, to assign a value in Col3

    Conditions
    ------------

    1. For each item in Col1, against the highest value in Col2, "Latest" to be assigned in Col3
    2. Col1 value should not be empty

    A sample of what I am trying to achieve is shown below..

    Col1 Col2 Col3
    A 1
    A 2
    A 3 "Latest"
    B 1
    B 2 "Latest"
    C 1
    C 2 "Latest"


    Thanks in advance

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    8,031

    Re: Loop to assign values based on condition

    Is the sample data truly representative of the real data? i.e. column A is always grouped together and column B is always in ascending numerical order?
    If so then you'd just need to check if any cell in A matches the cell below and if not then put Latest in C.

    BSB

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,027

    Re: Loop to assign values based on condition

    Perhaps?
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  4. #4
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Re: Loop to assign values based on condition

    Quote Originally Posted by BadlySpelledBuoy View Post
    Is the sample data truly representative of the real data? i.e. column A is always grouped together and column B is always in ascending numerical order?
    If so then you'd just need to check if any cell in A matches the cell below and if not then put Latest in C.

    BSB
    Column A will not be grouped together always
    Column B will always be in ascending order ( Corresponding to each item in A)

  5. #5
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Re: Loop to assign values based on condition

    Hi sintek,

    The code works when the items in Col A are grouped but fails when they are mixed up.
    Can you provide a workaround?

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,027

    Re: Loop to assign values based on condition

    Pls upload a sample file to make use of for testing.
    This works even if items in col 1 are mixed up...
    Please Login or Register  to view this content.
    Last edited by Sintek; 12-26-2017 at 08:29 AM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Loop to assign values based on condition

    Quote Originally Posted by chullan88 View Post
    A sample of what I am trying to achieve is shown below..

    Col1 Col2 Col3
    A 1
    A 2
    A 3 "Latest"
    B 1
    B 2 "Latest"
    C 1
    C 2 "Latest"


    Thanks in advance
    Please Login or Register  to view this content.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,027

    Re: Loop to assign values based on condition

    Excellent code jindon...Very clean & simplistic...thanks for the lesson

  9. #9
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Re: Loop to assign values based on condition

    Excellent..works great...

  10. #10
    Forum Contributor
    Join Date
    07-12-2015
    Location
    Dubai,UAE
    MS-Off Ver
    Office 365
    Posts
    447

    Re: Loop to assign values based on condition

    Thanks Jindon and sintek

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,027

    Re: Loop to assign values based on condition

    Tx for the rep...Although my code does produce same result, it is far inferior to jindons

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Assign Open Workbook To A Variable Based On A Condition
    By picton2000 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-25-2018, 06:26 PM
  2. Do loop In between two values until Condition met
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-09-2017, 12:52 PM
  3. Assign an agent to customer based on language and loop - VBA
    By origal in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-21-2015, 01:39 PM
  4. Loop: Assign value to Cell based on List
    By hamidxa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-16-2013, 12:38 AM
  5. More efficient way to loop through cells and assign values
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 40
    Last Post: 08-29-2013, 05:54 AM
  6. [SOLVED] VBA loop to copy a worksheet a number of times, assign name each time based on loop number
    By TBG in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2012, 10:54 PM
  7. Assign fixed values to array / For...Next loop
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-18-2011, 05:43 PM

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