2014-10-11 54 views
0

我有這個大型的Excel電子表格大約10,000行和5,000個唯一ID。我想要做一個基於數據庫的搜索和替換。在Excel中如何使用庫搜索和替換列上的字符串

FROM:

enter image description here

enter image description here

TO:

enter image description here

我知道這可以通過使用快速搜索一個完成nd替換爲excel,但如果數據有大約5,000個唯一的agent-id,那麼這可能是一項艱鉅的任務。

任何人都有一個明智的建議?

在此先感謝!

+0

什麼樣的替換是你的意思?對於性能明智的查詢,我只需在Excel中使用SQL:http://blog.tkacprow.pl/excel-sql-add-in-free/。它將比任何VBA更有效率。您也可以使用外部數據創建查詢 - >從其他來源 - > Microsoft Query – 2014-10-11 12:15:45

+0

我修改了示例。我知道,SQL可以很容易地做到這一點。但是,SQL在這裏不可用。 – 2014-10-11 12:19:58

+0

我不明白?意思在哪裏?在Excel中,您可以輕鬆使用SQL。還請具體說明你的意思。 – 2014-10-11 12:57:38

回答

1

我能夠做一個vb宏代碼來完成我所需要的。我可能需要調整一些特定的單元格,但它現在可行。

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 
+0

此代碼的一個問題是,如果沒有值匹配,一旦它試圖找到。我目前正在努力弄清楚。 – 2014-10-12 07:15:03

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中的值替換它們。

+0

感謝提示Bilbo Baggins – 2014-10-12 06:29:06