繁体   English   中英

Excel VBA:如何创建具有唯一范围和多个工作表的VLOOKUP?

[英]Excel VBA: How to create VLOOKUP with unique range and multiple worksheets?

我还是vba的新手,目前正在尝试编写代码,在其中从源工作表和输出工作表执行vlookup并将其放入我的输出工作表。 我需要对整个A列(每个月的行数有所不同)执行vlookup并将其放置在G列中。每当我尝试运行它时,它都会显示“错误438:对象不支持此属性或方法”。 我在网上某个地方找到了此代码,并尝试对其进行更改以匹配我的数据。 我猜我的sourceSheet没有分配给工作表,即使我指定了它也可以吗? 谁能帮忙:

这是我的代码:

Sub Vlookup()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Set sourceSheet = Worksheets("Data1")
Set outputSheet = Worksheets("Info2")
With sourceSheet
     SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With outputSheet
    OutputLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  'This is the line where I keep getting the error:
 .Range("G2:G" & OutputLastRow).Formula = _
             "=VLOOKUP(A2,'" & sourceSheet & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
    End With
    End Sub

您指的是工作表本身,而不是名称。 尝试:

.Range("G2:G" & OutputLastRow).Formula = _
         "=VLOOKUP(A2,'" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"

暂无
暂无

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

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