2

我正在编写一个宏,它根据现有工作表上的值创建可变工作表。我很好地管理了这部分,但现在我需要在另一个引用新创建的工作表的工作表上添加一个 VLOOKUP 公式。新工作表的名称没有固定模式,因此我无法引用它们。这是我用来创建新工作表的代码:

Dim ws As Worksheet
Dim rngCriteria As Range
Dim sName As String
Dim I As Long
Dim LastRow As Long

    LastRow = Cells(Rows.Count, 1).End(xlUp).Row

    With Sheets("Part Type REC")
        If .AutoFilterMode = True Then .AutoFilterMode = False

        .Range("D1:D" & LastRow).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("J1"), Unique:=True

        Set rngCriteria = .Range("J1").CurrentRegion

        For I = 2 To .Cells(Rows.Count, "J").End(xlUp).Row
            sName = .Cells(I, "J")
            Set ws = ThisWorkbook.Worksheets.Add
            ws.Name = sName
            .Range("D1:D" & LastRow).AutoFilter Field:=1, Criteria1:="=" & .Cells(I, "J").Value
            .Range("A1:H" & LastRow).SpecialCells(xlCellTypeVisible).Copy Destination:=ws.Range("A1")
        Next I

        .AutoFilterMode = False
    End With
    Sheets("Part Type REC").Select
    Columns("J:J").Select
    Selection.ClearContents
    Range("A1").Select

这是我需要引用新工作表的 VLOOKUP:

Sheets("TP Parts").Select
Range("O2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'ws.name'!C[-14],1,FALSE)"
Range("O2").Select

我哪里错了?

提前致谢!

4

1 回答 1

4

试试这个(未经测试 - 只是在这里输入

Range("O2").FormulaR1C1 = "=VLOOKUP(RC[-1]," & ws.name & "!C[-14],1,FALSE)"
于 2012-04-13T14:34:01.080 回答