过滤范围上的vlookup
我需要使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论