Vlookup用于填充从表1到表2的值

时间:2013-02-19 15:33:06

标签: excel excel-vba vba

我正在寻找使用Vlookup功能的VBA代码如下:

  1. 在“数据”表单中查找名为“热数据广告系列ID”的列,在其所有单元格中,它将实现vlookup公式

  2. Vlookup公式应包含:

    a)对于Lookup_value,它会在“数据”表中搜索名为“源代码”的列,并获取位于那里的值

    b)对于Table_array,它将转到“Vlookup”表并将前4列标记为表数组

    c)Col_index_num = 2

    d)Range_lookup = False

1 个答案:

答案 0 :(得分:1)

更改表格(“数据”)。选择包含数据的工作表的实际名称

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