Question

I have a column full of address from a state. In another column I have all districts of the state. I want to extract district out of the address. I tried different formulas, but didnt work.

For eg:

Address Column

S.V.N. Road, Warangal, Andhra Pradesh 

Vuyyur, Distt., Krishna, Andhra Pradesh  

Nagarjunasagar, Vijayapuri South,, Distt., Guntur   

Paritala (PO), Kanchikacherla, (MD), Krishna (DT)

Tekkali, Srikakulam Distt.,, Andhra Pradesh etc

Districts column

Warangal

Krishna

Guntur

Srikakulam etc

I want a formula or VBA code that searches Districts from districts column inside the address column and extract district. Please note Districts are not in a specific place in address Cell.

Was it helpful?

Solution

try this:

=IFERROR(INDEX(D$2:D$5,MATCH(1,IF(ISERROR(SEARCH(D$2:D$5,A2)),0,1),0),1),"District not in list")

Entered as array formula by pressing CTRL+SHIFT+ENTER.
Enter this adjacent to your first address.(below it is in B2 and address is on A2)
Change it to suit your need.
I assumed that your data looks like this.

sample image

As you can see, I put the District List on D2:D5 so in the formula, change it to where your list is.
If the no match is found, it will return District not in list as seen in B6.
I believe every address have a unique Disrtict?
If ever the adress contains two(2) Districts, it will return the first found District in the address.

How does the formula work?

=SEARCH(D$2:D$5,A2)

This searches each word found in D2:D5 (your districts) in address in A2.
The result is an array of error values and numbers.
Considering A2 it returns {#Value!, #Value!, 14, #Value!}.

To handle the errors we add ISERROR combined with IF.

=IF(ISERROR(SEARCH(D$2:D$5,A2),0,1)

This replaces #VALUE! with 0 and non-error value with 1.
Now it returns {0, 0, 1, 0}.

Now, you want to return the District of the corresponding address.
To do that we will use INDEX and MATCH combination.
First we use MATCH to know the row number of our District.
We already have this array {0, 0, 1, 0}, we need to match 1.

=MATCH(1,IF(ISERROR(SEARCH(D$2:D$5,A2),0,1),0)

Simplified:

=MATCH(1,{0, 0, 1, 0}, 0)

And yields 3.

Finally, we use INDEX to return the District of the corresponding address.

=INDEX(D$2:D$5,MATCH(1,IF(ISERROR(SEARCH(D$2:D$5,A2)),0,1),0),1)

Simplified:

=INDEX(D$2:D$5,3,1)

The last argument which is [column] can be omitted since you only have 1.
Or you can specify it as 1.

This yields to Warangal.
Note that we added IFERROR to handle non-existing District.
Hope this helps you a bit.

OTHER TIPS

I think you'll need a custom function that

  • Reads in the Address cell
  • Loops through comma delimited address section by section to get a substring
  • Does a lookup of the substring from a range containing a list of all possible districts
  • If found it will return the district to your District column, otherwise it will continue looping until you get to the end of the address contents

Input: S.V.N. Road, Warangal, Andhra Pradesh Lookup "S.V.N. Road" in districts Not found, continue Lookup "Warangal" in districts Found, enter in Districts column, end function

If possible, it would be helpful to have the address formatted regularly so the District would always appear after the Nth comma so you could simply select the Nth+1 substring for District.

Take the value of A Column and B column.

Dim storACellVal As Variant
Sub Test()
lngLastCell = Application.CountA(Columns(1))
for i= 1 to lngLastCell
Acellno = "A" & i
Bcellno = "B" & i
'It will store the datas of column A into aColVal
aColVal = Sheets("Sheet1").Range(Acellno).Value
'It will store the datas of column B into bColVal
bColVal = Sheets("Sheet1").Range(Bcellno).Value

storACellVal=split(aColVal,",")
for j=0 to UBound(storACellVal)
    if trim(storACellVal(j)=trim(bColVal) then
        msgbox "True"
    End if
next j
Next i
End Sub

I hope this might help you. I apologies, if I did anything wrong.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top