vba Vlookup 将值从工作表 1 填充到工作表 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14961215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vlookup to populate values from sheet 1 to sheet 2
提问by mgunia
I'm looking for VBA code which would use Vlookup function as following:
我正在寻找使用 Vlookup 函数的 VBA 代码,如下所示:
Look for a column called Hot Data Campaign ID in “Data” sheet and in all it's cells it will implement the vlookup formula
Vlookup formula should consist of:
a) For
Lookup_value
it would search for the column called Source Code in the “Data” sheet and take the values located thereb) For
Table_array
it will go to “Vlookup” sheet and mark first 4 columns as table arrayc)
Col_index_num = 2
d)
Range_lookup = False
在“数据”表中查找名为 Hot Data Campaign ID 的列,在所有单元格中,它将实现 vlookup 公式
Vlookup 公式应包括:
a) 因为
Lookup_value
它会在“数据”表中搜索名为 Source Code 的列并获取位于那里的值b) 因为
Table_array
它将转到“Vlookup”表并将前 4 列标记为表数组C)
Col_index_num = 2
d)
Range_lookup = False
回答by Ripster
Change Sheets("Data").Select to the actual name of the sheet containing your Data
更改工作表(“数据”)。选择包含您的数据的工作表的实际名称
Sub VLookupMacro()
Dim FormulaCol As Long
Dim LookupCol As Long
Dim TotalRows As Long
Dim TotalCols As Long
Dim i As Long
Sheets("Data").Select
TotalRows = ActiveSheet.UsedRange.Rows.Count
TotalCols = ActiveSheet.UsedRange.Columns.Count
For i = 1 To TotalCols
If Cells(1, i).Value = "Hot Data Campaign ID" Then FormulaCol = i
If Cells(1, i).Value = "Source Code" Then LookupCol = i
Next
Cells(2, FormulaCol).Formula = "=VLOOKUP(" & Cells(2, LookupCol).Address(False, False) & ",Vlookup!A:D,2,FALSE)"
Cells(2, FormulaCol).AutoFill Destination:=Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
With Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
.Value = .Value
End With
End Sub