2017-10-05 181 views
0

我想让我的查找范围动态,所以我使用变量col但我得到的是#N/A在结果列中。vba动态查找范围

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 

回答

2

col放在公式中意味着什么,因为公式不会解决什么col意味着什么。相反,您可以使用字符串连接来解析文本。

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

成为

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

感谢名单,它的工作 –