2016-07-26 133 views
0

我有2個Excel表,我想查找和替換值,但是我希望有多個替換值取一個匹配值的點。Excel查找並用多個新值替換單個匹配值

Sheet 1:        Sheet 2: 

Match Value       Match Value New Value 
28045000        28045000  28051560 
39162010        28045000  28056549 
39269000        39162010  39596000 

在片1中的所有匹配值是唯一的,而在片材2的匹配值可以具有重複的,因爲它們對應於多個新值。因此,如果工作表1和工作表2中的匹配值相同,那麼我想將工作表1中的匹配值替換爲與匹配值對應的所有新值。在故已被替代後,表1應該是這樣的:

Sheet 1:       

Match Value       
28051560 
28056549       
39596000        
39269000        

所以我們可以看到,28045000是由2個值,28051560和2個獨立的細胞28056549更換,而39162010是由39596000替代,而39269000在表2中沒有匹配值,保持不變。

我通常會手動執行此操作,但大約有30,000行數據,其中一些超過10個值與單個匹配值匹配。我有下面的代碼,但是,這並沒有正確替換所有新值的匹配值。有沒有一種方法可以讓Excel搜索兩張紙的整個範圍並自動進行適當的更改?

Sub multiFindNReplace() 
    Dim myList, myRange 
    Set myList = Sheets("sheet 1").Range("A1:A5000") 
    Set myRange = Sheets("sheet2").Range("A1:A5000") 
    For Each cel In myList.Columns(1).Cells 
     myRange.Replace what:=cel.Value, replacement:=cel.Offset(0, 1).Value 
    Next cel 
End Sub 
+0

必須在應用VBA這個? – pnuts

+0

@pnuts沒有不一定,有沒有不同的方式來做到這一點,你可以建議? – mike

+0

@pnuts可以詳細闡述一下更多細節中的匹配部分嗎? – mike

回答

0

我會做這樣的:

宏只是循環通過第一片並將其與第二片進行比較。如果匹配,則替換第一個值,然後添加c + 1並繼續搜索。因爲原始值被替換,那麼原始值存儲在d中,如果它發現第二個匹配,它不會因爲c + 1而替換它,它會轉到else子句,插入一行並將值放入新排。像這樣,它循環遍歷sheet1上的整個列。 PS:我希望你能理解它,我沒有那麼多時間,爲了更好的可讀性,稍後再編輯。

更新:

所以又來了,我加了MaxRow的計數器和overcomment它一個容易理解。

更新2:

與while循環

現在由於for循環不regconize極限改變

Sub CompareLoop() 

'Iterator Worksheet 1, is the counter for the ws1 column 
Dim iWS1 As Integer 
'Iterator Worksheet 2, is the counter for the ws1 column 
Dim iWS2 As Integer 
'Switch New Row, is the switch if the next value need a new row 
Dim sNR As Integer 
'Maximal Row Count, need to be extend when new rows are added 
Dim MaxRows As Integer 
'valueHolder, is the holder for the orginal value, the orginal value might be replaced on the sheet 
Dim valueHolder As Long 

'Worksheet1 
Dim ws1 As Worksheet 
'Worlsheet2 
Dim ws2 As Worksheet 

Set ws1 = ActiveWorkbook.Worksheets("table1") 
Set ws2 = ActiveWorkbook.Worksheets("table2") 

'Set iWS1 to the first row 
iWS1 = 1 
'Get MaxRows 
MaxRows = ws1.Cells(Rows.Count, 1).End(xlUp).Row 

'Loop through the Rows on WS1 setting switch to 0 and store the value  from the ws1 row in the holder 
While iWS1 <= MaxRows 
sNR = 0 
valueHolder = ws1.Cells(iWS1, 1).Value 

'Loop through the Rows on WS2, searching for a value that match with the value from ws1 
For iWS2 = 1 To ws2.Cells(Rows.Count, 1).End(xlUp).Row 
    'When it matches, then look if there was already a match with the value, if not replace it on the ws1 and increase the sNr to 1 
    If valueHolder = ws2.Cells(iWS2, 1).Value Then 
     If (sNR < 1) Then 
      ws1.Cells(iWS1, 1).Value = ws2.Cells(iWS2, 2).Value 
      sNR = sNR + 1 
     'When the sNR is already > 0, increase the Iterator for the ws1 that he will point on the new line 
     'increase the maxrows because we got one more soon, finally insert the new row and store the value from ws2 in it 
     Else 
      iWS1 = iWS1 + 1 
      MaxRows = MaxRows + 1 
      Range(ws1.Cells(iWS1, 1), ws1.Cells(iWS1, 1)).EntireRow.Insert 
      ws1.Cells(iWS1, 1).Value = ws2.Cells(iWS2, 2) 
     End If 
    End If 
Next iWS2 
iWS1 = iWS1 + 1 
Wend 

End Sub 
+0

這就是我要找的。我對代碼的一個問題是我需要多次運行代碼,因爲它只執行一個值,然後停止執行。例如,如果我有1500,與1501,1502和1503匹配,而我也有1700匹配1701和1702,我需要運行代碼兩次才能工作。這成爲問題,因爲我的工作表中有成千上萬的數據,並且我不知道執行代碼的次數。有沒有辦法編輯這段代碼,以便它可以在宏的一次運行中執行所有代碼? – mike

+0

我想這是來自我犯的一個錯誤。它得到了row.count,後來又增加了一些行,但是仍然停留在同一行。我將更正 – thentt

+0

我已嘗試更新副本,但我仍然遇到與上述相同的問題。任何你認爲它發生的原因? – mike

0

假設柱洗脫,開始是連續的和被標記,在表1 ,B2和向下複製到花色:

=IF(ISERROR(MATCH(A2,'Sheet 2'!A:A,0)),A2,"") 
含有選自表1列B的所有值

複製範圍和粘貼特殊,下面最後值條目表2列B.

複製工作表2列B爲表1和過濾器的A1,除去在A列中的空白刪除工作表1列B.