简体   繁体   English

vba动态vlookup范围

[英]vba dynamic vlookup range

I want to make my vlookup range dynamic, so I am using a variable col but all I am getting is #N/A in the result column. 我想使我的vlookup范围动态变化,所以我使用了一个变量col,但是我得到的只是结果栏中的#N / A。

Private Sub CommandButton1_Click()

path3 = Application.GetOpenFilename(Title:="Please browse Output tab excel file")
Set wbs3 = Workbooks.Open(path3)

Set aCell1 = wbs3.Sheets("Output").Range("A1:X1").Find(What:="Functions", LookIn:=xlValues, LookAt:=xlPart, _
MatchCase:=False, SearchFormat:=False)
col = aCell1.Column

lastrow1 = wbs3.Sheets("Analysis").Columns("A").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
With wbs3.Sheets("Analysis").Range("I2:I" & lastrow1)
    .Formula = "=VLOOKUP(H2,'Output'!col:col,1,0)"
    .Value = .Value
End With

End Sub

Putting col in the formula means nothing to it as the formula won't resolve what col means. col放在公式中对它没有任何意义,因为公式无法解决col的含义。 instead you could use string concatenation to resolve the text. 相反,您可以使用字符串串联来解析文本。

.Formula = "=VLOOKUP(H2,'Output'!col:col,1,0)"

becomes 变成

.Formula = "=VLOOKUP(H2,'Output'!" & col.address & ",1,0)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM