VBA code needed to search and find 2 values in a spreadsheet

Anell JvR

New Member
Joined
Dec 11, 2019
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I have a spreadsheet with a long list of countries of the world on the left.

The columns are then made up of different sickness types. Users must access the list to find the country and then, depending on the sickness type, find the rule that apply to that specific illness in that specific country.

They therefore have to scroll up and down a lot and it takes time to make sure they are viewing the correct rule in the correct row for that country.

The requirement is a search type function that will take the user to the cell where the country and the specific sickness type "meet".

I created a dropdown list for the countries and the sickness types by using Data Validation, and I used macros to get to the country successfully, move it up and to change the background colour of the cell so the country's name can stand out.

I however need to go to the cell applicable to the specific sickness type to find the rule for that country. Any suggestions will be welcome!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi

I would use Data > Advanced Filter.
Insert a few rows above your source data.
Type Country in A1 and Sickness in B1
Enter required Country in A2 and sickness type in B2 and run the code
VBA Code:
Sub FilterData()

    Range("A5:G26").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        Range("B1:C2"), Unique:=False

End Sub

Obviously change the range to match your range.
Either attach the macro to a button on the sheet, or place some event code behind the sheet so that if the value in A2 or B2 changes, the macro gets called.
 
Upvote 0
Hi

I would use Data > Advanced Filter.
Insert a few rows above your source data.
Type Country in A1 and Sickness in B1
Enter required Country in A2 and sickness type in B2 and run the code
VBA Code:
Sub FilterData()

    Range("A5:G26").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        Range("B1:C2"), Unique:=False

End Sub

Obviously change the range to match your range.
Either attach the macro to a button on the sheet, or place some event code behind the sheet so that if the value in A2 or B2 changes, the macro gets called.

Thank you very much Roger, much appreciated! For some reason I get an "Out of Memory" error but I'll review my code. I have not worked in VBA for a couple of years now and became so rusty! :oops:
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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