0

Is there any way to have a spreadsheet in Excel automatically update text based off a list on a separate sheet?

So, let's say every time I copy content into Sheet1 and would like it to automatically update the words Apple, Orange and Banana to their corresponding words on a list in Sheet2, what would be the best way?

I have a extensive list of branch codes which are assigned to their branch names but the data extracted from our system only provides the branch codes. Which is why I am trying to find a way to have this updated by default.

In the example below, I would like the content in the column Branch, which is in Sheet1, to be automatically replaced to what my list provides in another sheet, which is the second image below.

Content in Sheet1:

enter image description here

And Sheet2 with the list:

enter image description here

Thanks for your help!

8
  • Yes, absolutely this can be done. What have you already tried? Commented Feb 13, 2018 at 0:02
  • Please include some sample data (before and after) which at least closely matches your actual data (if it's not confidential/sensitive then post the actual data) Commented Feb 13, 2018 at 0:05
  • @TimWilliams Of course. Just a sec. I will add two screenshots. Commented Feb 13, 2018 at 0:09
  • @ChrisMelville I wasn't quite sure how to approach this. I know how to enter formulas into the individual cells to replace texts, but don't know how to create something that searches through an entire sheet and automatically replaces something based on a list. Commented Feb 13, 2018 at 0:13
  • 1
    If you're asking specifically for a VBA solution without posting any of your own attempts, then people here are generally inclined to downvote/vote to close. Commented Feb 13, 2018 at 0:44

1 Answer 1

1

After spending some more time searching for help, I came across this code which did the job for me:

Sub UpdateBranchNames()
    Dim r As Range
    With Sheets("Sheet2")
        For Each r In .Range("A1:A500", .Range("a" & Rows.Count).End(xlUp))
            Sheets("Sheet1").Columns(8).Replace r.Value, r(, 2).Value
        Next
    End With
End Sub

Thanks for the responses.

UPDATE:

Here is an updated version thanks to @QHarr.

Sub UpdateBranchNames()
    Dim r As Range
    Dim myRange as Range
    Dim myList as Worksheet
    Set myList = Sheets("Sheet2")
    Set myRange = myList.Range("A1:A500", myList.Range("a" & Rows.Count).End(xlUp))

    With myList
        For Each r In myRange
            Sheets("Sheet1").Columns(8).Replace r.Value, r(, 2).Value
        Next
    End With
End Sub 
Sign up to request clarification or add additional context in comments.

2 Comments

Put this .Range("A1:A500", .Range("a" & Rows.Count).End(xlUp)) into a variable e.g. Dim myRange As Range. Set myRange = Sheets("Sheet2") .Range("A1:A500", .Range("a" & Rows.Count).End(xlUp)) Also, consider putting Sheets("Sheet2") into a variable. It will be more efficient.
@QHarr Thanks for your help! I have added your suggestion to the answer above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.