Excel VBA : Applying Vlookup To filtered range

NiladriG

New Member
Joined
Apr 27, 2022
Messages
1
Office Version
  1. 2010
Platform
  1. Windows
Hi All,

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 ?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,223,631
Messages
6,173,465
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top