简体   繁体   中英

Excel Formula to Search One Cell and Replace Text in Another Cell

I've been searching all over and found a lot of posts that are almost what I need, but nothing exactly right. Hoping someone here can help.

I have two columns of data, column H contains broker names (Broker1, Broker2, etc). Column I contains the name of the company they rep (ABC, BBC, etc). These affiliations change often so I am looking for a way to setup a formula or macro for quick changes. Essentially what I want to do is say "If the cell I2 contains company ABC, change H2 to Broker2."

All the substitute and replace functions seem to be centered around replace all Broker1 with Broker2, but I need to be able to make this change only when a certain company is involved. Broker2 may rep 20 companies, so I need to be able to limit the find and replace to certain data elements.

Hope this all makes sense, thanks in advance for the help!

Adding details: Ok, so a real life example. A company named Elite represents Kellogg's. H2 = "Elite" and I2 = 'Kellogg's". However, Kellogg's may change over to another company named Core. So I need to go search I2:I200 and wherever we find "Kellogg's" replace H with "Core". So if I78 = "Kellogg's" I need to change H78 to "Core". Trouble is, Elite may rep 20 other companies who don't move, so I can't completely replace all of Elite with Core on my list, only in very certain circumstances

Rep Change

Excel

You could create a unique list of company names and add the representatives. Then you could use an INDEX/MATCH formula in the representative column ( H ), something like this:

在此处输入图片说明

Then you could change Elite in one place only ( M7 ) and it would automatically change in all the cells of the H column, eg H4 .

VBA

If you need it for one time operations, the following code might be helpful. Adjust the constants to fit your needs.

Sub RepChange()

    Const Company As String = "Kellog's"
    Const Representer As String = "Elite"
    Const NewRepresenter As String = "Core"

    Const cSheet As Variant = "Sheet1"
    Const cColRep As Variant = "H"
    Const cColComp As Variant = "I"
    Const cFR As Long = 2

    Dim LR As Long
    Dim i As Long

    With ThisWorkbook.Worksheets(cSheet)
        LR = .Columns(cColComp).Cells(.Rows.Count).End(xlUp).Row
        For i = cFR To LR
            If StrComp(.Cells(i, cColComp), Company, vbTextCompare) = 0 And _
                    StrComp(.Cells(i, cColRep), Representer, vbTextCompare) _
                    = 0 Then .Cells(i, cColRep) = NewRepresenter
        Next
    End With

    MsgBox "Operation finished successfully.", vbInformation, "Success"

End Sub

StrComp with vbTextCompare is used to avoid case-sensitivity ie AA=Aa=aa.

Or you could use this:

Sub RepChange2(Company As String, Representer As String, _
        NewRepresenter As String)

    Const cSheet As Variant = "Sheet1"
    Const cColRep As Variant = "H"
    Const cColComp As Variant = "I"
    Const cFR As Long = 2

    Dim LR As Long
    Dim i As Long

    With ThisWorkbook.Worksheets(cSheet)
        LR = .Columns(cColComp).Cells(.Rows.Count).End(xlUp).Row
        For i = cFR To LR
            If StrComp(.Cells(i, cColComp), Company, vbTextCompare) = 0 And _
                    StrComp(.Cells(i, cColRep), Representer, vbTextCompare) _
                    = 0 Then .Cells(i, cColRep) = NewRepresenter
        Next
    End With

End Sub

where you could write even several statements in a sub like this one:

Sub Kellogs()
    RepChange2 "Kellog's", "Elite", "Core"
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM