2016-02-29 83 views
0

我試圖根據另一個文檔上的映射表更新列表中的單元格。每個月,我都會得到一份我需要這樣做的清單。映射表將是恆定的,但源數據會發生變化,所以我有這樣的事情基於映射表的VBA宏更新單元格

Column A Column B 
Name  Description 
Test1  Test for SO 
Test2  Test2 for SO 
... 
Test100 Test100 for SO 

我將有一個相應的映射表,我想宏A列中每個單元格評估針對下面的映射表並更新到映射表第二列中的值(位於不同的工作表上),有沒有辦法做到這一點?

Column A Column B 
Name  Real Name 
Test1  Fund1 
Test2  Fund2 
... 
Test100  Fund100 

基本上,用於數據片(第一片材)的最終輸出將是:預先

Column A Column B 
Name  Description 
Fund1  Test for SO 
Fund2  Test2 for SO 
... 
Fund100  Test100 for SO 

感謝。我已經列出了兩列中列A的範圍,但我不知道哪些函數可以執行此操作?我開始爲每個陳述做一些事情,但被困住了。

+0

是一個單獨的表映射表?你不能只使用一個vlookup或匹配+索引? –

+0

我需要它在一個宏中沒有創建一個vlookup的新列 – fordo19

回答

2

你可以試試這個:

Option Explicit 

Sub map() 

Dim SourceData As Worksheet: Set SourceData = ThisWorkbook.Sheets("Sheet1") 'Change the name of the sheet 
Dim Mapping As Worksheet: Set Mapping = ThisWorkbook.Sheets("Sheet2") 'Change the name of the sheet 

Dim SourceDataLstr As Long, MappingLstr As Long 
Dim i As Long, j As Long 
Dim RawDataKey As String, MappingKey As String 

SourceDataLstr = SourceData.Range("A" & Rows.Count).End(xlUp).Row 'Find the lastrow in the Source Data Sheet 
MappingLstr = Mapping.Range("A" & Rows.Count).End(xlUp).Row 'Find the lastrow in the Mapping Sheet 

With SourceData 

For i = 2 To SourceDataLstr 

RawDataKey = .Cells(i, "A").Value 

     For j = 2 To MappingLstr 

       MappingKey = Mapping.Cells(j, "A").Value 

       If MappingKey = RawDataKey Then 
        .Cells(i, "A").Value = Mapping.Cells(j, "B").Value 
        Exit For 
       End If 

     Next j 

Next i 

End With 

End Sub 
+0

嗨,感謝您的幫助,我想我可能已經解釋了這一點很糟糕,我想要做的更新是SourceData表 - 不是第三張表表Sheet 3)。我可以更新這個以使Sheet1在IF塊中更新嗎?或者有沒有第三張紙的理由? – fordo19

+0

我只是假設你想保留原始數據,看我的編輯 – manu

+0

哇,這太棒了。非常感謝。 – fordo19