2017-10-05 240 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

感謝名單,它的工作 –