我的宏无法执行vlookup

  • 本文关键字:执行 vlookup excel vba
  • 更新时间 :
  • 英文 :


我有一个复合体,它正在进行大量的复制粘贴到不同的文件,但是,我在下面的行中遇到了一个错误,错误是对象不支持此属性或方法

.Range("Q" & i).Value="if(P" & i & "=" & Chr(34) & "Y" & Chr(34) & ",vlookup(A" & i & "," & OutShVar & "!$A:$BP,68,0)," & Chr(34) & "Not Available" & Chr(34) & ")"

(我正在复制粘贴与问题相关的部分。(

Set OutShVar=ThisWorkbook.Worksheets("MasterReport")
Set RngConcat=OutShVar.Range("A:A")
Set wMain=Workbooks.open(ForePath & sfn)
Call OpenLink 'Performs tasks on another report after opening it

'After doing a bunch of things on the OpenLink report,
'i want to do a vlookup on that report that will take things from the excel
'workbook where the macro is i.e., OutShVar and RngCon'
'On Master list/Refresh
if .Range("P" & i).Value = "N" Then
.Range("Q" & i).Value="Not inscope"
Else
If not IsError(Application.Match(.Range("A" & i).Value,RngConcat,0) Then
.Range("Q" & i).Value="if(P" & i & "=" & Chr(34) & "Y" & Chr(34) & ",vlookup(A" & i & "," & OutShVar & "!$A:$BP,68,0)," & Chr(34) & "Not Available" & Chr(34) & ")"
'This is where the problem is, not sure if this is right way to do the vlookup?

若要分配公式,请使用Range Object的formula属性。此外,使用"工作表名称"属性可以使用它,而不是公式中的工作表对象。

下面的代码对您来说应该很好。

.Range("Q" & i).Formula = "= IF(P" & i & "=" & Chr(34) & "Y" & Chr(34) & ",VLOOKUP(A" & i & ",'" & OutShVar.Name & "'!$A:$BP,68,0)," & Chr(34) & "Not Available" & Chr(34) & ")"

以下是供未来参考的范围的关键属性之间的差异:

CCD_ 1给出一个字符串,表示在屏幕上显示的单元格内容。正在使用。文本通常是个坏主意,因为你可能会得到####

Value2为您提供单元格的基本值(可以是空、字符串、错误、数字(双精度(或布尔值(

Value为您提供与相同的。值2,除非单元格格式为货币或日期,否则它会为您提供VBA货币(可能会截断小数位数(或VBA日期。

来源:另一个StackOverflow答案

最新更新