How to do Vlookup using VBA to sheet which is filtered?

dmadhup

Board Regular
Joined
Feb 21, 2018
Messages
146
Office Version
  1. 365
Hi,

I am doing vlookup to filtered sheet using this code:

VBA Code:
Sub test()
Sheets("Sheet1").Select
Range("B2:B6").SpecialCells(xlVisible).FormulaR1C1 = "=VLOOKUP(rc1,Sheet2!c1:c3,2,FALSE)"
End Sub

This code works well when sheet1 is filtered. But In my case Sheet2 is filtered not sheet1.

I want to do vlookup from sheet1 (Not filtered) to the sheet2 (filtered). Is there any way I can modify my code to achieve this?

Any help is appreciated.
Thank you!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Looks to me you just need to swap sheet1 with sheet2 and vice versa and make sure the ranges are correct.
 
Upvote 0
I meant in the formula, swapping sheets in the formula. I don't see why you can't do it in the formula. Anyway, it'd help if you could post some data.
 
Upvote 0
I meant in the formula, swapping sheets in the formula. I don't see why you can't do it in the formula. Anyway, it'd help if you could post some data.

Hi,
Below is the attached image of Sheet1 and Sheet2 Data where I want to do vlookup from sheet1 to sheet2 (Filtered)
 

Attachments

  • Sheet1.PNG
    Sheet1.PNG
    4.9 KB · Views: 6
  • Sheet2.PNG
    Sheet2.PNG
    6.1 KB · Views: 6
  • Sheet2 before filter on C column.PNG
    Sheet2 before filter on C column.PNG
    6.2 KB · Views: 6
Upvote 0
It turns out to be more involved than I Thought. Try this:

VBA Code:
Sub test()

Range("B2").FormulaArray = "=INDEX(Sheet2!$B$2:$B$6,MATCH(1,IF(SUBTOTAL(3,OFFSET(Sheet2!$B$2:$B$6,ROW($A$2:$A$6)-ROW($A$2),0,1))>0,IF($A$2:$A$6=$A2,1)),0),0)"
Range("B2").Copy Destination:=Range("B3:B6")

End Sub
 
Upvote 0
It turns out to be more involved than I Thought. Try this:

VBA Code:
Sub test()

Range("B2").FormulaArray = "=INDEX(Sheet2!$B$2:$B$6,MATCH(1,IF(SUBTOTAL(3,OFFSET(Sheet2!$B$2:$B$6,ROW($A$2:$A$6)-ROW($A$2),0,1))>0,IF($A$2:$A$6=$A2,1)),0),0)"
Range("B2").Copy Destination:=Range("B3:B6")

End Sub


Hello,

It catches wrong value. For example my sheet2 value is this:

1591886730387.png


Then I am getting this on sheet1

1591886795048.png


the price for part 222 should be 1000 not 600.

Thank you!
 
Upvote 0
When I tested it, it showed #N/A if the corresponding row in sheet2 was filtered off. I got this:

9
#N/A
11
12
13

By the way, my Excel is having problems. I probably need to reinstall Office. Can't test it before reinstalling.
 
Upvote 0
When I tested it, it showed #N/A if the corresponding row in sheet2 was filtered off. I got this:

9
#N/A
11
12
13

By the way, my Excel is having problems. I probably need to reinstall Office. Can't test it before reinstalling.


Hi,
I see.
But I tested it without applying filter on sheet2. Means if not filtered value is there it suppose to catch it.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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