I have a script to find and replace BTO bird abbreviations for their common name i.e. "BC" is "Blackcap". I have tried two approaches , both using using an array. It works on the whole though I have had to use full stops (periods) in some cases, but with some interesting results.
This is what I am trying to replace on Sheet1. Not all rows are the same length.
7Apr 2CD R. 2B.-fm
8Apr C. MG 1B.-m
9Apr 2BT 2MG 1B.-m 2R. WP C. CD
and this is the result, just columns A-D
07-Apr 2Crowollared Dunnockove 2Collared Dove Robin
08-Apr Crow. Crow Magpie
09-Apr 2Blue Tit 2Blue Tit 2Magpie
Column B is a duplicate of C but with errors. Column C is what should have appeared in column B. The rest of the columns are fine. I am new to vba and modified a script I found on the internet but am struggling after many hours to put this right. I suspect the problem is with the loop.
The Script
Sub FindReplaceAll()
Application.ScreenUpdating = False
Dim v As Variant, i As Long
v = Array("B.", "Blackbird", "BC", "Blackcap", "BT", "Blue Tit", "BF", "Bullfinch" _
, "CG", "Canada Goose", "CH", "Chaffinch", "CC", "Chiffchaff", "CT", "Coal Tit" _
, "CD", "Collared Dove", "BZ", "Common Buzzard", "C.", "Crow", "D.", "Dunnock" _
, "GO", "Goldfinch", "GS", "Great Spotted Woodpecker", "GT", "Great Tit" _
, "GF", "Greenfinch", "H.", "Grey Heron", "HM", "House Martin", "HS", "House Sparrow" _
, "JD", "Jackdaw", "J.", "Jay", "K", "Kestrel", "LR", "Lesser Redpoll" _
, "LT", "Long Tailed Tit", "MG", "Magpie", "M.", "Mistle Thrush", "NH", "Nuthatch" _
, "PE", "Peregrin Falcon", "KT", "Red Kite", "RE", "Redwing", "R.", " Robin" _
, "SK", "Siskin", "ST", "Song Thrush", "SH", "Sparrowhawk", "SG", "Starling" _
, "SD", "Stock Dove", "SL", "Swallow", "SI", "Swift", "WW", "Willow Warbler" _
, "WP", "Wood Pigeon", "WR", "Wren")
For i = LBound(v) To UBound(v) - 1 Step 2
ActiveSheet.UsedRange.Replace v(i), v(i + 1), , , vbTextCompare
Next i
Application.ScreenUpdating = True
End Sub
Help would be gratefully appreciated.
Bookmarks