Bizarre behaviour in find and replace macro (help!)

Oliver Dewar

Board Regular
Joined
Apr 17, 2011
Messages
201
Hello!

I have a macro that goes through the values in a certain range and replaces 'tokens' I've set up (e.g. +State+) with the corresponding value from the current records column.

For all the instances where the new text is one or two words everything works beautifully. But for the tokens that should be replaced by a string of html anchorlinks I get "Error 13 type mismatch"... and that's not all... one token is apparantly totally missed in the sequence!

Here's a chunk of my code:

Code:
Range("AT5:AT15").Replace What:="+Locality+", Replacement:=Cells(ActiveCell.Row, "C").Value, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

Range("AT5:AT15").Replace What:="+State+", Replacement:=Cells(ActiveCell.Row, "D").Value, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
    If Cells(ActiveCell.Row, "G").Value = 7 Then
        Range("AT5:AT15").Replace What:="+Suburb-links+", Replacement:=Cells(ActiveCell.Row, "K").Value, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

        Else

        Range("AT5:AT15").Replace What:="+Loc-Backlink+", Replacement:=Cells(ActiveCell.Row, "Q").Value, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    End If
    
Range("AT5:AT15").Replace What:="+Serv-links+", Replacement:=Cells(ActiveCell.Row, "L").Value, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

So at the moment the if is testing false and the +Loc-Backlink+ token is the one that causes the error 13. The +Serv-Links+ token is the one that is missed altogether. I've double checked the spelling and it's correct.

Any ideas?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You get Error 13 when you try to do maths on non-numeric things, and +Loc-Backlink+ certainly looks as though it's inviting VBA to do maths with it. (I don't know why it would actually do that though.)

Have you tried using symbols which don't have a mathematical connotation, if only to eliminate that possibility?
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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