'INDIRECT function in excel gives #NAME?

I have a sheet named "Countries" as shown in Screenshot_1. The data is populated from backend (C# code). B2:B6 represent names for named range that refers to cells in another sheet named "States" shown in ScreenShot_2. Named Ranges are also added from the backend code.

In the main sheet I have cascading dropdowns for Country and States. States get loaded when I select a country from Country dropdown. States dropdown has a data validation mentioned below.

=INDIRECT(INDEX(Countries!$A$2:$B$6, MATCH(MainSheet!$B2, Countries!$A$2:$A$6, 0), 2)).

Note that there are no states for Country4 in states sheet. Hence, there is no named range defined for B5.

Till now everything works fine. If there are states linked to countries, they get loaded in states dropdown. Else the dropdown will be empty.

Countries States

Now the actual problem is, I have a requirement that says "Apart from selecting the values from the states dropdown, users must also be allowed to enter their own values". This works fine when dropdown has some values. But when I try to type values when the dropdown is empty, I get below error.

Is there a way to prevent this error from showing up?

Can someone please help me out here? Thanks in Advance.

PS: I cannot make any changes to backend code. This has to be handled in excel.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source