vba动态范围问题

时间:2017-10-17 11:40:55

标签: vba excel-vba excel

With newexcel.Sheets("Analysis")
    .Range("I1").AutoFilter Field:=9, Criteria1:="<>#N/A"
    .Range("E1").AutoFilter Field:=5, Criteria1:="InActive"
    .Range("D1").AutoFilter Field:=4, Criteria1:=(BLANK)
    .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy newexcel.Sheets("Test").Range("A1")
     firstRow139 = .AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Row
End With

With newexcel.Sheets("Analysis").Range(newexcel.Sheets("Analysis").Cells(firstRow139, 4), newexcel.Sheets("Analysis").Cells(lastRow139, 4))
    .Formula = "=VLOOKUP(A&firstRow139,'Test'!A:D,4,0)"
End With

在vlookup中,它没有采用firstRow139值

1 个答案:

答案 0 :(得分:1)

您需要将变量firstRow139置于公式的"部分之外。

更改您当前的公式,如:

.Formula = "=VLOOKUP(A&firstRow139,'Test'!A:D,4,0)"

为:

.Formula = "=VLOOKUP(A" & firstRow139 & ",'Test'!A:D,4,0)"

我可能会更改您使用With的方式的结构:

With newexcel.Sheets("Analysis")
    .Range(.Cells(firstRow139, 4), .Cells(lastRow139, 4)).Formula = "=VLOOKUP(A" & firstRow139 & ",'Test'!A:D,4,0)"
End With