在excel上如何搜索&使用库替换列上的字符串

时间:2014-10-11 11:46:34

标签: excel excel-vba search replace vba

我有大约10,000行和5,000个唯一ID的大型Excel电子表格。我想要做一个基于数据库的搜索和替换。

发件人:

enter image description here

enter image description here

enter image description here

我知道这可以使用快速搜索并在excel上替换,但如果数据有大约5,000个唯一的代理ID,那么这可能是一项艰巨的任务。

任何人都有明智的建议吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

以下是一些VBA,它将根据另一张表中的值列表在一张表中查找和替换。

Sub multiFindandReplace()
Dim myList, myRange
Set myList = Sheets("config").Range("A1:B9")
Set myRange = Sheets("Sheet1").Range("B2:I1000")
For Each cel In myList.Columns(1).Cells
myRange.Replace What:=cel.Value, Replacement:=cel.Offset(0, 1).Value, LookAt:=xlPart
Next cel
End Sub

这是在sheet1中搜索单元格B2:I1000。配置表包含2列,要在A列中查找的值,如果找到它们,则将使用该行的B列中的值替换它们。

答案 1 :(得分:1)

我能够制作一个vb-macro代码来完全按照我的需要做。我可能需要调整一些特定的细胞,但它现在可以使用。

Sub FindAndReplace()
' FindAndReplace Macro
' @author Louie Miranda
' Ability to find the range of ids against another worksheet
' and insert the name on the main sheet
'
    ' Loop over the current worksheet
    For Each c In Worksheets("RECORDS").Range("A3:A7").Cells

        ' Go to Agents sheet
        Sheets("AGENTS").Select

        ' Do a search
        Cells.Find(What:=c, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        Application.CutCopyMode = False

        ' Choose beside the column to copy
        ActiveCell.Offset(rowOffSet:=0, columnOffset:=-1).Activate
        Selection.Copy

        ' Back to records sheet
        Sheets("RECORDS").Select

        ' Paste on the current row, plus arrange on which row/offset
        Range(c.Address).Offset(0, 1).Select
        ActiveSheet.Paste

    Next c
End Sub