SternHammer
New Member
- Joined
- Apr 7, 2020
- Messages
- 7
So I am very new to VBA (though I am modertaely famuiliar with Excel) but I cannot for the life of me figure out why this isn't working.
I have a range of cells with addresses and postcodes in them, though some have 2 lines in the street address and some 3:
12 Easy Street
Lazy Town
ZX3 5PD
Or
15 Hard Street
Lazy Town
Nothingshire
ZX7 4PD
What I want to do is have the macro search the cell looking for the pattern [Capital Letter][Capital Letter][Number] and delete only the matching text (anything after the matching text being deleted is also fine).
My plan was to use Like to match the pattern and then use range.replace (or similar, I haven't worked this part out yet) to replace the matched pattern with a space (to effectively delete it). What I have so far is:
This is just returning a "check fail" for every cell in the range (so I haven't even looked at how to link the Like to the replace). It works fine when the postcode is the first text in the cell, but as soon as there is other text in front of it it fails. This feels like I am missing something very obvious, shouldn't Like check the entire string for the pattern? It's driving me nuts.
I have a range of cells with addresses and postcodes in them, though some have 2 lines in the street address and some 3:
12 Easy Street
Lazy Town
ZX3 5PD
Or
15 Hard Street
Lazy Town
Nothingshire
ZX7 4PD
What I want to do is have the macro search the cell looking for the pattern [Capital Letter][Capital Letter][Number] and delete only the matching text (anything after the matching text being deleted is also fine).
My plan was to use Like to match the pattern and then use range.replace (or similar, I haven't worked this part out yet) to replace the matched pattern with a space (to effectively delete it). What I have so far is:
VBA Code:
Sub PatternMatch()
Dim cell As Range
For Each cell In Range("E7:E10")
If cell Like "[A-Z][A-Z]#*" Then
Debug.Print cell
Else: Debug.Print "Check Fail"
End If
Next
End Sub
This is just returning a "check fail" for every cell in the range (so I haven't even looked at how to link the Like to the replace). It works fine when the postcode is the first text in the cell, but as soon as there is other text in front of it it fails. This feels like I am missing something very obvious, shouldn't Like check the entire string for the pattern? It's driving me nuts.