VBA Remove Text String in List based on Criteria in another List

xcellrodeo

Board Regular
Joined
Oct 27, 2008
Messages
209
Hello Mr Excel Community, I would like to ask for help with finding a suitable VBA Script to achieve the following:

Description of Problem:
In Tab called 'Addresses',
I have a list of Addresses in Column A (2000 Rows) which take the format of something like this: 'Flat 1 Main Street Brooks London'.
This address string, namely the flat nr, street name, district name and town are all contained in one cell and have only a space as separator.
In Tab called 'Districts', I have a list of 70 District names in Column A (70 Rows).

Result: What I would like to do is to delete all the Districts in the text strings in 'Addresses' sheet in Col. A which are based on the List found in the 'Districts' tab.
So for example, if in Tab 'Addresses', a text string says 'Flat 1 Main Street Brooks London', and the District 'Brooks' is found in the 'Districts' tab, I want it removed from the text string in the Tab 'Addresses'.
Hope this makes sense.

Thanks for any suggestions.
++Please note that above address is a fictitious address and not part of list++
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi,
Try this out and let me know if that's what you need. I set up both district and addresses header row as 1. If the header rows are different adjust the constant values accordingly (Distr_HeaderRow, Addr_HeaderRow).


VBA Code:
Sub AddressesRemoval()
    Dim wsDistricts As Worksheet
    Dim wsAddresses As Worksheet
    Dim i&
    Dim j&
    Dim lrDistr&
    Dim lrAddresses&
    Dim strDistrict$
    Dim strCurrentAddress$
    Dim strNewAddress$
    
    Const Distr_HeaderRow% = 1
    Const Addr_HeaderRow% = 1
    Const Distr_DefCol$ = "A"
    Const Addr_DefCOl$ = "A"
    
    Set wsDistricts = Worksheets("Districts")
    Set wsAddresses = Worksheets("Addresses")
    
    lrAddresses = wsAddresses.Range(Addr_DefCOl & Cells.Rows.Count).End(xlUp).Row
    lrDistr = wsDistricts.Range(Distr_DefCol & Cells.Rows.Count).End(xlUp).Row
    
    If lrAddresses <= Addr_HeaderRow Or lrDistr <= Distr_HeaderRow Then
        MsgBox "Lack of data to remove districts form addresses!", vbExclamation, "InfoLog"
        Exit Sub
    End If
    
    For i = Distr_HeaderRow + 1 To lrDistr
        lrAddresses = wsAddresses.Range(Addr_DefCOl & Cells.Rows.Count).End(xlUp).Row
        strDistrict = CStr(wsDistricts.Cells(i, Distr_DefCol))
        
        For j = Addr_HeaderRow + 1 To lrAddresses
            strNewAddress = vbNullString
            strCurrentAddress = CStr(wsAddresses.Cells(j, Addr_DefCOl))
            strNewAddress = Replace(strCurrentAddress, strDistrict, "", 1)
            
            strNewAddress = Replace(strNewAddress, LCase(strDistrict), "", 1)
            strNewAddress = Replace(strNewAddress, UCase(strDistrict), "", 1)
            wsAddresses.Cells(j, Addr_DefCOl) = strNewAddress
        Next j
    Next i
    
    Set wsDistricts = Nothing
    Set wsAddresses = Nothing
    
    MsgBox "Done"
    
End Sub
 
Upvote 0
Solution
Thanks Sebastian for this.
Can you tell me if the above will work irrespective of case sensitivity so for example if the district is listed in lower or upper case letters or has a mix?
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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