+ Reply to Thread
Results 1 to 5 of 5

Find and Replace Formula (VLOOKUP Maybe?) Based on Cell Values

  1. #1
    Registered User
    Join Date
    06-28-2022
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    3

    Find and Replace Formula (VLOOKUP Maybe?) Based on Cell Values

    Hi, I have two columns: product_id and list_price. Each product has a unique list_price and its own product_id, but appears multiple times. However, many of the rows have a product_id of 0, they are sorted from 0 to 5 lowest to highest. I need to replace the product_id cells that have a 0 in them with the correct product_id , which can be found in the same column by using the list_price column value. How do I do this for thousands of rows without having to do it manually?
    Attached Files Attached Files

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: Find and Replace Formula (VLOOKUP Maybe?) Based on Cell Values

    Do you mean that when there is a 0 product ID, if you look for that same price later in the list, it will match the actual product ID? One challenge is that you have many zeroes with the same price so you have to find the first non-zero product ID with that price.

    Is it guaranteed that no two products can ever have the same price?

    You can do this by adding another column with the result, then copying values back to column 1.

    C2 and fill down:

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    06-28-2022
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    3

    Re: Find and Replace Formula (VLOOKUP Maybe?) Based on Cell Values

    This might seem ballsy of me, Jeff, but could you explain it to me? I tried it, it works great, but I'd love an explanation. I am studying to be an analyst and struggle with formulas.

  4. #4
    Registered User
    Join Date
    06-28-2022
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    3

    Re: Find and Replace Formula (VLOOKUP Maybe?) Based on Cell Values

    Yeah I tried it on the larger dataset and it doesn't work. I wonder why

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: Find and Replace Formula (VLOOKUP Maybe?) Based on Cell Values

    Very reasonable question.

    I am going to work from the outside in and give the segments names

    =INDEX(OFFSET(A$1,MATCH(1,A:A,0)-1,0,COUNTIF(A:A,">=1")),MATCH(B2,OFFSET(B$1,MATCH(1,A:A,0)-1,0,COUNTIF(A:A,">=1"))))

    COUNTIF(A:A,">=1") nonzero_cell_count
    This counts how many cells in column A have a value of 1 or more. This is so we can count the non-zero cells.

    MATCH(1,A:A,0) row_with_first_1
    The row number of the row in column A that has the first 1

    OFFSET is a function that takes the initial range, moves it by the specified number of rows and columns, then sets the height of the result (number of rows). It can also set the width in columns but I am not using that here.

    OFFSET(A$1,MATCH(1,A:A,0)-1,0,COUNTIF(A:A,">=1"))
    Using our names we get
    OFFSET(A$1,row_with_first_1-1,0,nonzero_cell_count) all_nonzero_product_IDs
    So this starts with A1, and then moves down to the row with the first 1, and includes all the rows with nonzero numbers.

    OFFSET(B$1,MATCH(1,A:A,0)-1,0,COUNTIF(A:A,">=1")) prices_for_nonzero_product_IDs
    This is just like the previous one, but gives us the corresponding prices in column B.


    MATCH(B2,OFFSET(B$1,MATCH(1,A:A,0)-1,0,COUNTIF(A:A,">=1")))
    Replacing the name we get
    MATCH(B2,prices_for_nonzero_product_IDs) desired_price_row
    This finds the row number with the price ID we're looking for

    Now back to our main formula

    INDEX(OFFSET(A$1,MATCH(1,A:A,0)-1,0,COUNTIF(A:A,">=1")),MATCH(B2,OFFSET(B$1,MATCH(1,A:A,0)-1,0,COUNTIF(A:A,">=1"))))
    Replacing our names we get
    INDEX(all_nonzero_product_IDs,desired_price_row))

    This takes all nonzero product IDs, and returns the one in the row with the desired price.

+ 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. Replies: 3
    Last Post: 01-18-2021, 11:35 AM
  2. [SOLVED] Formula to find cell values based on criteria
    By corinereyes in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 02-20-2020, 09:16 PM
  3. Formula Help - Find and Replace based on cell value
    By mnfez in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-07-2016, 04:07 PM
  4. Find and replace multiple values based on criteria
    By samuelhzb in forum Excel General
    Replies: 6
    Last Post: 01-04-2015, 01:25 PM
  5. Macro to find all specific values in column and replace adjacent cell values
    By dblock02 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2013, 06:03 AM
  6. Help with find and replace formula or vlookup?
    By brooksc29 in forum Excel General
    Replies: 1
    Last Post: 08-11-2010, 10:10 PM
  7. Replies: 0
    Last Post: 10-13-2005, 02:05 PM

Tags for this Thread

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