-1

我正在寻找将使用 Vlookup 函数的 VBA 代码,如下所示:

  1. 在“数据”表中查找名为 Hot Data Campaign ID 的列,在它的所有单元格中,它将实现 vlookup 公式

  2. Vlookup 公式应包括:

    a) 因为Lookup_value它会在“数据”表中搜索名为 Source Code 的列并获取位于那里的值

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

    C)Col_index_num = 2

    d)Range_lookup = False

4

1 回答 1

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
于 2013-02-19T15:59:39.130 回答