excel - 通过命名范围进行 Vlookup
问题描述
我有一本工作簿,其中有一个主工作表和其余工作表。基本上,我的代码所做的是循环遍历未命名为“Main”的工作簿中的工作表并提取值并将它们放入主工作表中的适当列中。IsError 检查 Sheetname 是否已存在于列表中,如果存在 - 它不会添加值。工作表的布局非常混乱,有许多合并的单元格 - https://imgur.com/cayZXUA。
因此,在每张表中(主要除外),我都有包含“工资单”、“综合社会税”和“杂项支出”值的块。我想出了在每张工作表中给这些块命名为“假设”的想法,并且我为每张工作表设定了范围,以便可以在所有工作表中重复“假设”并且我可以遍历它。这是这个块的图像 - https://imgur.com/nPYyLbM。
代码运行但我得到#VALUE!我的主工作表上的错误 - https://imgur.com/a/H2TOFmW 我很抱歉提供链接,还没有发布图片的权限
这是代码本身:
Sub Sheets()
Dim wsheet As Worksheet
With ThisWorkbook.Sheets("Main")
For Each wsheet In ThisWorkbook.Sheets
If wsheet.Name <> "Main" Then
Set nextEntry = .Cells(.Rows.Count, "G").End(xlUp).Offset(1, 0)
Set nextEntry_payroll = .Cells(.Rows.Count, "AI").End(xlUp).Offset(1, 0)
Set nextEntry_consolidated_social_tax = .Cells(.Rows.Count, "AJ").End(xlUp).Offset(1, 0)
Set nextEntry_miscellaneous_expenditures = .Cells(.Rows.Count, "AK").End(xlUp).Offset(1, 0)
If IsError(Application.Match(wsheet.Name, .Range("G:G"), 0)) Then
nextEntry.Value = wsheet.Name
nextEntry_payroll.Value = wsheet.Application.VLookup("payroll", "Assumptions", 3, 0)
nextEntry_consolidated_social_tax.Value = wsheet.Application.VLookup("consolidated social tax", "Assumptions", 3, 0)
nextEntry_miscellaneous_expenditures.Value = wsheet.Application.VLookup("miscellaneous expenditures", "Assumptions", 3, 0)
End If
End If
Debug.Print wsheet.Name
Next wsheet
End Sub
解决方案
将 VLookup 函数从
wsheet.Application.VLookup("payroll", "Assumptions", 3, 0)
至
Application.VLookup("payroll", wSheet.Range("Assumptions"), 3, 0)
另外两个也是如此。
或者,您可以使用 VLookup 工作表函数来更改它,而不是计算 VBA 中的值:
nextEntry_payroll.Formula = "=VLookup(""payroll"", " & wsheet.Name & "!Assumptions, 3, 0)"
另外两个也是如此。试试看有什么不同。
推荐阅读
- laravel - 窗口机器不支持 FFPROBE
- kubernetes-helm - 继承 helm 模板
- python - 如何从需要在单独文件夹中读取和写入 .txt 文件的 .py 文件创建 python 可执行文件
- amazon-web-services - 从 GoDaddy 转移到 AWS Route53 后域无法正常工作
- c++ - 将光标自行移动到当前位置
- java - 无法在 IntelliJ IDEA 中导入 spring boot maven 项目 - 导入后 IDE 没有响应
- python - 如何在pydatatable中输入数据框列?
- docker - 如何从 docker 容器派生“docker run”语法
- reactjs - 如何在 React+bootstrap 中添加背景图片?
- date - 如果日期小于今天 +30,请使用 Google 表格