+ Reply to Thread
Results 1 to 10 of 10

vba search for value replace if not found

  1. #1
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    vba search for value replace if not found

    Hello - I have a worksheet with columns A - AF. In column A is employee number, column B is region (of the 8000 rows, there are four regions) and column U has supervisor id. I'm looking for some vba code that would check the value in U2 in column A:A and if not found, replace the value in U2 with another supervisor number, based off the offset value of column B. If supervisorID is not in column A, then IF value in B2 = "ab-us", then replace u2 with "0012345". If value in B2 = "ab-fr", then replace U2 with "0098765", if value in B2 = "ab-xyz" then replace U2 with "0065432", if value in b2 = "ab-378" then change U2 value to "00223344", etc.

    If the value is found, then do nothing.

    This would then repeated to the lastrow, etc.

    Thanks for reading!

    5/1/2017 - Adding a sample file attachment...!
    Attached Files Attached Files
    Last edited by Ironman; 05-01-2017 at 11:03 AM. Reason: Adding sample file

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,481

    Re: vba search for value replace if not found

    Hi Ironman

    Perhaps a sample workbook would help us assist you.
    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!!!

  3. #3
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: vba search for value replace if not found

    Quote Originally Posted by sintek View Post
    Hi Ironman

    Perhaps a sample workbook would help us assist you.
    So B2 value does not match U2 value and B2 = "ab-us", U2 is changed to 0012345 and A2 still does not match U2. What happens?

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,481

    Re: vba search for value replace if not found

    Is this what you mean.....
    Please Login or Register  to view this content.
    Last edited by Sintek; 04-30-2017 at 04:50 AM.

  5. #5
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: vba search for value replace if not found

    Thank you sintek, I've added a sample file. I tried your suggested code and every value in column U changed to '046903', not just the ones that were not there. The attachment has 8 cells in column U that are highlighted in light green - it is these cells I'd expect to see change to 046903 as the value in column B is "en-US". I added the "NEXT I" to your code.

    Please Login or Register  to view this content.
    Any thoughts on why it may not be working as planned? thanks again!!!
    Last edited by Ironman; 05-01-2017 at 11:37 AM.

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

    Re: vba search for value replace if not found

    K let me try to understand...With your sample
    If U2 = A2 then do nothing...Is this right?
    If U2 does not = A2 then
    If B2 = "en-US" then change U2 to "046903"

  7. #7
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: vba search for value replace if not found

    Close....if the value in U2 is not in column A:A, then the offset value for U would be '046903'. Also need to keep that leading '0'. Basically the file contains a list of employees and their supervisor (via id numbers). If a given supervisor is no longer with the company and no longer found in column A as an employee, then the corresponding value in U needs to be updated/changed to '046903'. The file is used as an upload to an application and it will error out on upload if there is a supervisor ID that is not also in column A as an employee. Hope that makes sense!

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

    Re: vba search for value replace if not found

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2010
    Posts
    190

    Re: vba search for value replace if not found

    Thank you jindon. Your code worked for me, but I have to admit, not sure what exactly going on. For example you are referencing column V in your rng = section. Also I'm unfamiliar with the specialcells and what that is doing. Additionally I would need to repeat this code for column AA, AB, AC and AD. Would making a copy and changing the "-19" to "-25" (for AA), "-26" (for AB), etc?

    Thoughts? Thanks again!

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

    Re: vba search for value replace if not found

    Confusing...

    If you upload a workbook with before/after showing clear logic behind it, it would help.

+ 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. Search for value in a range and overwrite if found and create new if not found
    By mm671750 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-04-2016, 01:19 PM
  2. Macro to search cells for substring and replace contents of cell if substring is found
    By robbyvegas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2015, 06:40 AM
  3. [SOLVED] Search within a column and repeat the search until all items are found
    By RANDY LIPOSKY in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-09-2014, 10:23 AM
  4. Replies: 2
    Last Post: 09-13-2013, 11:02 AM
  5. [SOLVED] find and replace to not replace characters found as wildcards
    By sabutler4 in forum Excel General
    Replies: 4
    Last Post: 07-03-2013, 06:48 PM
  6. [SOLVED] Macro ignores search code when 1 of the 2 search parameters is not found
    By BrodyNoochie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-25-2012, 05:21 PM
  7. [SOLVED] Replace object causes an error if search text not found
    By Scott in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-26-2006, 03:15 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