简体   繁体   English

使用可变查找值、可变查找范围 VBA 在过滤范围中进行 Vlookup

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

I am trying to apply Vlookup on a filtered range with Variable Lookup Value(Changing according to the row number) and Variable Lookup Range(From a user browsed workbook).我正在尝试使用可变查找值(根据行号更改)和变量查找范围(来自用户浏览的工作簿)在过滤范围上应用 Vlookup。 But, the formula bar after running the code shows the formula as :-但是,运行代码后的公式栏将公式显示为:-

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

I am not sure where these "@" signs are coming from.我不确定这些“@”标志是从哪里来的。 The lookup value for this particular row is :-Sri Lanka15-a_One-Time Base Rent.此特定行的查找值是:-Sri Lanka15-a_One-Time Base Rent。

Below is the code:-以下是代码:-

    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

Since i need to apply subsequent filters after this.因为我需要在此之后应用后续过滤器。 i would like to keep the lookup value as variable.我想将查找值保留为变量。

I have tried WorksheetFunction.Vlookup too, but i am not sure how to define the range from a file chosen by user in worksheetfunction我也尝试过 WorksheetFunction.Vlookup,但我不确定如何从用户在 worksheetfunction 中选择的文件中定义范围

Any help is highly appreciated !!任何帮助表示高度赞赏! Thanks谢谢

Please, try replacing of请尝试更换

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

with

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