excel - 我的宏无法执行 vlookup
问题描述
我有一个复杂的东西,它正在做很多复制粘贴到不同的文件,但是,我在下一行收到一个错误,错误是对象不支持这个属性或方法
.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?
解决方案
要分配公式,请使用范围对象的公式属性。此外,使用 Worksheet Name 属性来使用它,而不是公式中的工作表对象。
下面的代码应该适合您。
.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) & ")"
以下是 range 的关键属性之间的区别,以供将来参考:
. Text
给出一个字符串,表示单元格在屏幕上显示的内容. 使用 .Text 通常是个坏主意,因为您可能会得到 ####
. Value2
为您提供单元格的基础值(可以是空、字符串、错误、数字(双精度)或布尔值)
. Value
为您提供与 .Value2 相同的内容,除非单元格被格式化为货币或日期,它为您提供 VBA 货币(可能会截断小数位)或 VBA 日期。
资料来源:另一个 StackOverflow 答案
推荐阅读
- google-cloud-platform - SNMP 陷阱的 Stackdriver 监控警报
- sql - 如何在 sql 中使用 OPENJSON
- java - 比较不同格式的两个日期
- docker - 关于docker run命令参数的问题,-t -i
- c# - 获取“A”的第一次出现和“B”的最后一次出现之间的子字符串
- laravel - 如何在 Laravel 5.8 中更新产品
- python - 隐藏小部件后调用 size() 时应用程序大小不会改变
- python - 获取单个 G Suite 电子邮件帐户的 gmail.compose 权限以通过 GMail API 发送电子邮件
- mysql - 为什么使用游标的 MySQL 触发器只获取一个值
- dropwizard - ScheduledExecutorService 停止计划的可运行任务,然后在一段时间后重新运行正常