VBA: Search for Text strings and return value from table

ChetManley

New Member
Joined
Dec 9, 2019
Messages
33
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

Long time lurker, first time poster - let me thank you all for years of troubleshooting and learning with pro's how to be a bit better in Excel.

My question is around VBA - not very well versed in it - and will try to explain as best as I can.

I have a list of countries with a score against each country, let's call it "Countries" table.
I also have a list of customers with multiple columns for address, nationality, etc that all revolve around location, let's call this table "Customers".

What I would like to learn how to do (if possible) is to:

Run a VBA code to search each relevant column in the "Customers" table for the countries in the "Countries" table and return the country score for each respective customer on a separate table, let's call this one "Customer Score".

Is this possible at all? Or am I better off using formulas? Issue is I will have to do this on a quarterly basis and I'd like to make it as resilient as possible (in case of absence)..

Much appreciated,
Chet
 
Not sure if this can help you.
If on Sheet 3 you have the country, then the customer number is not necessary, check the following:

Book1
AB
1CountryRating
2AlbaniaHigh
3ArgentinaHigh
4AndorraMedium
5AutraliaLow
6AustriaLow
7AzerbaijanHigh
Sheet1



Book1
ABC
1Customer No.CountryRating
2123ArgentinaHigh
3123ArgentinaHigh
4123AutraliaLow
5345AlbaniaHigh
6567AustriaLow
7100AndorraMedium
Sheet3
Cell Formulas
RangeFormula
C2:C7C2=VLOOKUP(B2,Sheet1!$A$1:$B$7,2,0)
Hi, unfortunately no..

The issue is that the Country values that I would need to rate according to the Country Rating table, are all spread across different columns and there's no logic behind it.
If the address data was always in the same column then it'd work, issue is they're spread out across the Address1, Address2, Address3, etc.

I would like to find a solution to check each of those fields for each customer and return the country rating from the Rating table to a new table.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi, unfortunately no..

The issue is that the Country values that I would need to rate according to the Country Rating table, are all spread across different columns and there's no logic behind it.
If the address data was always in the same column then it'd work, issue is they're spread out across the Address1, Address2, Address3, etc.

I would like to find a solution to check each of those fields for each customer and return the country rating from the Rating table to a new table.

Also a caveat, the Customer number is required because we are rating customers based on their country information...
 
Upvote 0
Hi @DanteAmor I think I might be on to something - I'm using:

=SUMPRODUCT(--(COUNTRYRATING[Country]=Sheet1!E8:N8))>0

Where E:N are the various fields where I'm searching for the country string - it is working but returns TRUE or FALSE.
Any way I can return the High, Medium, Low values instead? Or if too complicated, return the country name found?

Thank you for the help!

Cheers
Chet
 
Upvote 0
I'm sorry, but I'm not understanding what you want to look for.
With the data you put. You could explain a couple of examples, that is, what data do you have, where do you want to look for it, what do you expect to get, then where do you want to search with what data do you want to look for and finally the expected result.
That is, tell me the sequence of steps you would do manually with a couple of cases.
 
Upvote 0
Hi @DanteAmor

I essentially have 2 tables of data - one with Customer Number and some details related to nationality, country of operation and 8 fields for address lines (where the address is broken up in different cells).

The second table has a list of countries and a rating associated with them.

The goal is to rate each customer, on a new table, based on their nationality, country of operation and address (where country is present) - my biggest problem is the Address fields because the country may appear in either of the 8 Address fields.


Customer No.AddressLine1AddressLine2AddressLine3AddressLine4AddressLine5AddressLine6AddressLine7
12xx11Finland2233
23ddSpainxxxx112244

I tried using this formula
=SUMPRODUCT(--(COUNTRYRATING[Country]=Sheet1!E8:N8))>0

Where "COUNTRYRATING[Country]" is the column with the Country name in the Country Rating table
And
Where "Sheet1!E8:N8" is the range for one of the customers address lines (like the example table above).

The problem is that the formula can only return TRUE or FALSE, if you can teach me a formula that returns the Country Name when matched in the Address cells I would be eternally thankful

Hope this makes it clearer?

I realize the requirements have changed a bit since the first post but I've tried to find my own solution....

Many thanks for your time,
Chet
 
Upvote 0
Assuming your data in the "Country rating" sheet like this:

Book1
AB
1COUNTRYSCORE
2FinlandHight
3SpainMedium
4Argentinalow
Country rating


And also assuming your data on sheet1 in this way.
I put the formula in column C.

Book1
ABCDEFGHIJK
1
7CustomerNo.ScoreAddressLine1AddressLine2AddressLine3AddressLine4AddressLine5AddressLine6AddressLine7
812xxHight11Finland2233
923ddMediumSpainxxxx112244
1045yylow11111Argentina1
Sheet1
Cell Formulas
RangeFormula
C8:C10C8{=INDEX(COUNTRYRATING[SCORE],SUMPRODUCT(--(COUNTRYRATING[COUNTRY]=Sheet1!E8:L8)*(ROW(COUNTRYRATING[COUNTRY])))-1)}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Hi @DanteAmor - that's great! Any chance that there is a tweak to pick up the country if it's part of a cell with more text than just the country name?
e.g.

Formula works perfect if country name is alone in a cell, like below:
Street XYZ, 111France

But if it has text around it it returns #VALUE, like below the below examples:
MadridSpain, Calle 1111
Street 222Paris, France
Street 33, United Kingdom, London

Is there any way to tweak the formula to be able to pick up the country string if it has more text in the same cell?

Many many thanks
 
Upvote 0
Try this
Book1
AB
2FinlandHight
3SpainMedium
4Argentinalow
5United KingdomMedium
Country rating


Book1
ABCDEFGHIJK
1
7CustomerNo.ScoreAddressLine1AddressLine2AddressLine3AddressLine4AddressLine5AddressLine6AddressLine7
812xxHight11New Finland2233
923ddMediumSpain, Madridxxxx112244
1045yylow11111La Argentina de 1
1150ssMedium22Street 33, United Kingdom, London2222
Sheet1
Cell Formulas
RangeFormula
C8:C11C8{=INDEX(COUNTRYRATING[SCORE],MAX(IF(ISNUMBER(SEARCH(COUNTRYRATING[COUNTRY],Sheet1!E8:L8)),ROW(COUNTRYRATING[COUNTRY])))-1)}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.
 
Upvote 0
Solution
Try this
Book1
AB
2FinlandHight
3SpainMedium
4Argentinalow
5United KingdomMedium
Country rating


Book1
ABCDEFGHIJK
1
7CustomerNo.ScoreAddressLine1AddressLine2AddressLine3AddressLine4AddressLine5AddressLine6AddressLine7
812xxHight11New Finland2233
923ddMediumSpain, Madridxxxx112244
1045yylow11111La Argentina de 1
1150ssMedium22Street 33, United Kingdom, London2222
Sheet1
Cell Formulas
RangeFormula
C8:C11C8{=INDEX(COUNTRYRATING[SCORE],MAX(IF(ISNUMBER(SEARCH(COUNTRYRATING[COUNTRY],Sheet1!E8:L8)),ROW(COUNTRYRATING[COUNTRY])))-1)}
Press CTRL+SHIFT+ENTER to enter array formulas surrounded with curly braces.

Solution verified!

Fantastic job @DanteAmor - you have my many thanks!
 
Upvote 0

Forum statistics

Threads
1,224,945
Messages
6,181,937
Members
453,074
Latest member
JefersonKollet

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