簡體   English   中英

使用可變查找值、可變查找范圍 VBA 在過濾范圍中進行 Vlookup

[英]Vlookup in filtered Range with Varaible Lookup Value, Variable Lookup Range VBA

我正在嘗試使用可變查找值(根據行號更改)和變量查找范圍(來自用戶瀏覽的工作簿)在過濾范圍上應用 Vlookup。 但是,運行代碼后的公式欄將公式顯示為:-

=IFERROR(VLOOKUP(@Sri Lanka15-@a_One-@Time Base Rent,'[C_Rent Report_25082020.xlsx]Sheet 1'!$J$1:$N$968,4,0)," ")

我不確定這些“@”標志是從哪里來的。 此特定行的查找值是:-Sri Lanka15-a_One-Time Base Rent。

以下是代碼:-

    Dim LR As Long   ' Defined as Last row in source file
    Dim nlr As Long   'Defined as Last row in Macro Workbook where vlookup is applied
    Dim Filename As String
    Filename = Application.GetOpenFilename(FileFilter:="All Files(*.xls; *.xlsx; *.csv),*xls,*.xlsx, *csv", Title:="Select File To Be Opened")
    Workbooks.Open Filename:=Filename
    sourcefile = Dir(Filename)
    With ActiveSheet
       Range("A1:AQ" & nlr).AutoFilter Field:=25, Criteria1:="One-Time Base Rent"
       For Each cell In Range("AA2:AA" & nlr).SpecialCells(xlCellTypeVisible)
            lookupvalue = Cells(cell.Row, "Z").Value
            cell.Formula = "=IFERROR(VLOOKUP(" & lookupvalue & ",'[" & sourcefile & "]Sheet 1'!$J$1:$N$" & LR & ",4,0),"" "")"    ' The problem seems to be here in lookup value as rest are appearing as fine in formula
       Next
             
    End With

因為我需要在此之后應用后續過濾器。 我想將查找值保留為變量。

我也嘗試過 WorksheetFunction.Vlookup,但我不確定如何從用戶在 worksheetfunction 中選擇的文件中定義范圍

任何幫助表示高度贊賞! 謝謝

請嘗試更換

cell.Formula = "=IFERROR(VLOOKUP(" & lookupvalue & ",'[" & sourcefile & "]Sheet 1'!$J$1:$N$" & LR & ",4,0),"" "")" 

cell.Formula = "=IFERROR(VLOOKUP(" & cells(cell.Row, "Z").Address & ",'[" & sourceFile & "]Sheet 1'!$J$1:$N$" & lr & ",4,0),"" "")"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM