过滤范围上的vlookup

发布于 2025-01-24 14:45:15 字数 1125 浏览 2 评论 0原文

我需要使用VBA在过滤范围内进行Vlookup。我经历了在同一主题上发布的不同主题,但是他们实际上无法回答我的问题。

基本上,我需要执行查找的工作表,将通过用户输入接收。我的代码如下:

Option Explicit

Sub VlookATM()

Dim lookFor As String
Dim srchRange As Range
Dim PMTPathWs As Workbook
Dim book2 As Workbook
Dim CurrentWs As Worksheet, WorkersWs As Worksheet
Dim ATMpathWs As Worksheet
Dim CurrentLastRow As Long, WorkersLastRow As Long, lastrow As Long, X As Long, d As Integer
Dim workerRange As Range
Dim path As String



path = InputBox("Please enter the ATM file path with extension")
Set ATMpathWs = Workbooks.Open(Filename:=path)
Set CurrentWs = ThisWorkbook.Worksheets("Current_Data")
Set WorkersWs = ThisWorkbook.Worksheets("Active_Workers")

CurrentLastRow = CurrentWs.Range("A" & Rows.Count).End(xlUp).Row
ATMpathWs.Activate

CurrentWs.UsedRange.AutoFilter 4, "ATM Testing", xlFilterValues

ActiveCell.Resize(lastrow - ActiveCell.Row + 1).SpecialCells(xlVisible).FormulaR1C1 = _
"=VLOOKUP(RC[-5],'[ATMpathWs]Charge Type Wise Effort Report'!R9C5:R23C9,5,0)"



End Sub

当我运行代码时,大多数情况下它没有丢弃任何错误,在文件中,我可以看到行被过滤,但是查找未完成。另外,有时代码丢弃运行时错误。

有人可以对此提供一些启示吗?

I need to vlookup on a filtered range using VBA. I have gone through different threads posted on same topic, however they can't actually answer my question.

Basically the sheet where I need to perform the lookup, will be received via User input. My code goes as follows :

Option Explicit

Sub VlookATM()

Dim lookFor As String
Dim srchRange As Range
Dim PMTPathWs As Workbook
Dim book2 As Workbook
Dim CurrentWs As Worksheet, WorkersWs As Worksheet
Dim ATMpathWs As Worksheet
Dim CurrentLastRow As Long, WorkersLastRow As Long, lastrow As Long, X As Long, d As Integer
Dim workerRange As Range
Dim path As String



path = InputBox("Please enter the ATM file path with extension")
Set ATMpathWs = Workbooks.Open(Filename:=path)
Set CurrentWs = ThisWorkbook.Worksheets("Current_Data")
Set WorkersWs = ThisWorkbook.Worksheets("Active_Workers")

CurrentLastRow = CurrentWs.Range("A" & Rows.Count).End(xlUp).Row
ATMpathWs.Activate

CurrentWs.UsedRange.AutoFilter 4, "ATM Testing", xlFilterValues

ActiveCell.Resize(lastrow - ActiveCell.Row + 1).SpecialCells(xlVisible).FormulaR1C1 = _
"=VLOOKUP(RC[-5],'[ATMpathWs]Charge Type Wise Effort Report'!R9C5:R23C9,5,0)"



End Sub

When I run the code, most of the time it is not throwing any error and in the file I can see the rows are filtered, however the lookup is not done. Also, there are times when the code is throwing runtime error.

Can anyone please shed some light on this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文