Excel VBA - Vlookup

Excel VBA - Vlookup

我有两部作品sheet(sheet1 和sheet2)。两者都包含一个带有 header "ID" 的列(列并不总是在相同的位置,因此需要找到)。

需要在 "ID" 列之前的新列中进行 vlookup。

这是我目前得到的结果

sub vlookup ()

    FIND COLUMNS WITH "ID"-HEADER

        'Set variables for Column Sku
        'note: cfind1 is for sheet 1 and cfind 2 is for sheet 2

            Dim col As String, cfind1 As Range, cfind2 As Range

            column = "ID"

            Worksheets(1).Activate
            Set cfind1 = Cells.Find(what:=column, lookat:=xlWhole)

            Worksheets(2).Activate
            Set cfind2 = Cells.Find(what:=column, lookat:=xlWhole)


    'CREATE COLUMN WITH VLOOKUP

            'activate worksheet 1
            Worksheets(1).Activate

            'add column before sku-column
            cfind1.EntireColumn.Insert

            'Select cell 1 down and 1 to left of sku-cell.
            cfind1.Offset(1, -1).Select

            'Add VlookUp formulas in active cell
            ActiveCell.Formula = "=VLOOKUP(LookUpValue, TableArray,1,0)"

                '(Lookup_Value should refer to one cell to the right 
                 (= cfind1.Offset (1, 0)??)

                'Table_Array should refer to the column in sheet(2) with header "id"


             'Autofill Formula in entire column
                 '???
End Sub

一切正常,直到 "vlookup-part" 我设法在正确的单元格中输入了一个公式,但我就是无法使用该公式。

如何在同一个 sheet 中将 lookup_value 设置为 "one cell to the right" "table_array" 作为 header "ID" 在 worksheet(2)?

中的列

我怎样才能最终在整个列中自动填充 vlookup 公式?

如果有人能帮助我解决正确的 vlookup 公式/变量和自动填充问题,那就太好了。

以前没有这样做过,但我的方法是使用单元格或 range.formula 属性 并构建要在单元格中写入的字符串。例如:

myrange.formula = "=Vlookup("&Lookup_Value&","&Table_Array&","&Col_index_num&","&Range_Lookup&")"

如果您想避免使用工作表,您也可以使用类似于下面的内容

curr_stn = Application.WorksheetFunction.VLookup(curr_ref, Sheets("Word_Specifications").Range("N:O"), 2, False)

Valuse/variables 当然需要更改。 lookup_value,数组(范围),列号,精确匹配。

精确匹配需要 false,相似匹配需要 true

尝试下面的完整代码

Sub t()

Dim col As String, cfind1 As Range, cfind2 As Range

            Column = "ID"

            Worksheets(1).Activate
            Set cfind1 = Cells.Find(what:=Column, lookat:=xlWhole)

            Worksheets(2).Activate
            Set cfind2 = Cells.Find(what:=Column, lookat:=xlWhole)


    'CREATE COLUMN WITH VLOOKUP

            'activate worksheet 1
            Worksheets(1).Activate

            'add column before sku-column
            cfind1.EntireColumn.Insert

            'Select cell 1 down and 1 to left of sku-cell.
            cfind1.Offset(1, -1).Select

            'Add VlookUp formulas in active cell
            LookUp_Value = cfind1.Offset(1, 0).Address(False, False)
            Table_Array = Col_Letter(Worksheets(2).Cells.Find(what:=Column, lookat:=xlWhole).Column) & ":" & Col_Letter(Worksheets(2).Cells.Find(what:=Column, lookat:=xlWhole).Column)
            ws_name = Worksheets(2).Name
            Col_index_num = 1
            Range_Lookup = False
            ActiveCell.Formula = "=VLOOKUP(" & LookUp_Value & ", " & ws_name & "!" & Table_Array & ", " & Col_index_num & ", " & Range_Lookup & ")"

            'Autofill Formula in entire column
            lastrow = Range(cfind1.Address).End(xlDown).Row
            Range(cfind1.Offset(1, -1).Address).AutoFill Destination:=Range(cfind1.Offset(1, -1).Address & ":" & Col_Letter(cfind1.Offset(1, -1).Column) & lastrow), Type:=xlFillDefault


End Sub


Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function