+ Reply to Thread
Results 1 to 4 of 4

Replace value of cells in sheet1 with list of values in sheet2 identified by sheet1 value

  1. #1
    Registered User
    Join Date
    10-09-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    13

    Replace value of cells in sheet1 with list of values in sheet2 identified by sheet1 value

    I am looking for a macro to place on an Excel sheet that would replace all values in a range of columns that have a corresponding list of values in another worksheet that each have the same identifier. So Sheet1 contains several columns with various values.

    A B C D
    1 PRD LFT DEC
    2 DEC STF PRD

    Sheet2 contains 2 columns. In Column1 there are values that correspond to some of the values
    A B
    1 PRD test1
    2 PRD test2
    3 PRD test3
    4 DEC sys1
    5 DEC sys2

    Clicking on the macro button would replace PRD in A1 & C2 in Sheet1 with "test1, test2, test3" and DEC in D1 & A2 with "sys1, sys2" and so on for all values that exist in Sheet 2. There could be values in Sheet1, like LFT, that do not exist in Sheet2 so these would be skipped

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Replace value of cells in sheet1 with list of values in sheet2 identified by sheet1 va

    can you attach a sample file (or paste a link to) with data and desired result ?
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    10-09-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Replace value of cells in sheet1 with list of values in sheet2 identified by sheet1 va

    Solved using the following:

    Sub ReplaceCodes()
    Dim cel As Range
    Dim strCode As String
    Dim rngFound As Range
    Dim strAddress As String
    Dim strReturn As String
    Application.ScreenUpdating = False
    For Each cel In Worksheets("Sheet1").UsedRange ' or a specific range
    strCode = cel.Value
    If strCode <> "" Then
    With Worksheets("Sheet2").Range("A:A")
    Set rngFound = .Find(What:=strCode, LookAt:=xlWhole, _
    After:=.Cells(.Cells.Count))
    If Not rngFound Is Nothing Then
    strReturn = rngFound.Offset(ColumnOffset:=1).Value
    strAddress = rngFound.Address
    Do
    Set rngFound = .FindNext(After:=rngFound)
    If rngFound.Address = strAddress Then Exit Do
    strReturn = strReturn & ", " & _
    rngFound.Offset(ColumnOffset:=1).Value
    Loop
    cel.Value = strReturn
    End If
    End With
    End If
    Next cel
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Replace value of cells in sheet1 with list of values in sheet2 identified by sheet1 va

    Hi, kevinkusman,

    you should wrap up your code with code-tags according to Forum Rule #3.

    You should combine the bits from Sheet2 in order to replace the search item in one turn like
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ 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: 6
    Last Post: 07-25-2013, 02:58 PM
  2. fetching values from sheet1 and putting in drop down list in sheet2
    By szpt9m in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-19-2013, 06:43 AM
  3. find and replace sheet2 list in sheet1
    By shrinivasmj in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2013, 08:17 AM
  4. delect values in Sheet1 and copy values from Sheet2 and then paste in Sheet1
    By drpramanik in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-02-2011, 05:28 AM
  5. Checking the Cells in Sheet1 with Cells in Sheet2 and replace
    By Checking the cells in Sheet1 with Sheet2 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-19-2006, 04:29 AM

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