Search and Replace custom function issue

NewOrderFac33

Well-known Member
Joined
Sep 26, 2011
Messages
1,283
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Good morning, I'm trying to write a function to search and replace, thus:
Code:
Set AnimalColumn = Sheets("Database").Range("Col_AnimalType")
ReplaceWithSpecies AnimalColumn

Function ReplaceWithSpecies(RangeToProcess As Range)
    With RangeToProcess
        Select Case UCase(.Formula)
            Case Is = "Tiger", "Lion"
                .Formula = "Cat"
            Case "Labrador", "Alsatian"
                .Formula = "Dog"
            Case Else
                .Formula = "Fish"
        End Select
    End With
End Function

but when I attempt to run my code. I get "Runtime error 13 Type Mismatch" with the "Select Case" line highlighted.

Can anyone suggest where I'm going wrong, please?

Thanks in advance - Pete
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Sorry, just realised this should be:
Code:
Set AnimalColumn = Sheets("Database").Range("Col_AnimalType")
ReplaceWithSpecies AnimalColumn

Function ReplaceWithSpecies(RangeToProcess As Range)
    With RangeToProcess
        Select Case UCase(.Formula)
            Case Is = "TIGER", "LION"
                .Formula = "Cat"
            Case "LABRADOR", "ALSATIAN"
                .Formula = "Dog"
            Case Else
                .Formula = "Fish"
        End Select
    End With
End Function
but it still returns the same error!
Also, all the values in the worksheet range being processed are text - no numeric values.
Pete
 
Last edited:
Upvote 0
Is Col_AnimalType more than one cell?
 
Upvote 0
Hi, Rory - yes - there are about 12,000 cells within the range.
Pete
 
Upvote 0
You need to loop each cell in the "Col_AnimalType", and pass the cell as the argument.
And since you have ~12K cells then it's better to turn off Application.ScreenUpdating.

Code:
Dim c As Range
Application.ScreenUpdating = False
    For Each c In Sheets("Database").Range("Col_AnimalType")
        ReplaceWithSpecies (c)
    Next
Application.ScreenUpdating = True
 
Upvote 0
Hi, Akuini - thanks for your reply - although your suggestion would work, I'm currently trying to avoid For Each loops as there are another 10 similar operations that need to be performed on my workbook and they can be quite slow.
 
Upvote 0
You're going to need a loop of some kind. I suggest you load the range into an array and loop through that, then write the array back to the sheet.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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