VBA Custom Function referring to two closed lists

Will from London

Board Regular
Joined
Oct 14, 2004
Messages
217
Hi

I am attempting to write a custom function to validate the Complementary Identification Code (CIC) for a list of over 10,000 financial assets.

The CIC is a 4 character alpha-numeric code and I have an exhaustive list of permitted options for the first two characters and an exhaustive list of permitted options for the final two. There are 251 for the first and 112 for the second so I would prefer not to have to type them all into the VBA function. These two lists are stored as columns (text) within the same Excel workbook (but on a different tab) as where the formula needs to be used. I've already allowed for cases where the CIC is not exactly 4 characters and where the letters are not in uppercase.

For example in cell A1 I have "FR11" and I want cell B1 to show "OK" (as both "FR" and "11" are in the respective lists) but if I have "FR10" in cell A2 I want B2 to return "Error" (as the "10" is not permitted in the second list).

Any help will be much appreciated.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,714
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
One way would be something like:

Excel Formula:
=IF(COUNT(MATCH(LEFT(A1,2),list1,0),match(RIGHT(A1,2),list2,0))=2,"OK","Error")
 

Will from London

Board Regular
Joined
Oct 14, 2004
Messages
217
Hi. Thanks for your reply. I was specifically looking for something I could incorporate in a custom function (as it does other things like check that the letters are in uppercase) but your suggestion is not case sensitive.

I have tried variations on the following:
In a cell I have:
=CICcheck(A1,INDIRECT(References!$B$5),INDIRECT(References!$E$5))
Where References!B5 has the text of the range e.g. "References!B9:B259" and
Where References!E5 has the text of the range e.g. "References!E9:F120" and

VBA Code:
Function CICcheck(valToTest as string, range1 as variant, range2 as variant) As String
dim cicFirstTwo as String

cicFirstTwo = left(valToTest,2)
  'Other parts of code checking it's 4 characters, uppercase letters etc
'then the part that is not working:

 If iserror(worksheetfunction.vlookup(cicFirstTwo, range1, 2, false)) = True then
      CICcheck = "Error"
endif

'...
End function

But if the worksheetfunction.vlookup(cicFirstTwo, range1, 2, false) part is an error then the custom function returns "#VALUE!" rather than processing the "CICcheck = "Error"" line.

Thanks
 

Will from London

Board Regular
Joined
Oct 14, 2004
Messages
217
Actually RoryA, thanks to your suggestion of COUNT I've solved it using if worksheet function.countif(...) = 0 then...
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,129,889
Messages
5,638,852
Members
417,055
Latest member
SerenaCruz

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
Top