Multiple string search

jgresko

New Member
Joined
Jun 2, 2011
Messages
9
I am trying to search column L for letters "N-V" and if found replace it with the letter "P". I have been successful searching for one letter and changing it to "P", but don't want to have tons of code if not needed. Thanks!!

Code:
Sub Change_ReturnType()
'Changes return type N-V to P'
i = 2
Sheets("Listed").Select
Do While Range("A" & i) <> ""
If InStr(1, Range("L" & i), "N-V") > 0 Then
Range("L" & i) = "P"
End If
i = i + 1
Loop
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this:
Code:
Sub Change_ReturnType()
    
    ' Set reference: Tools -> References -> Microsoft VBScript Regular Expressions 5.5
    
    Dim i As Integer, sh As Worksheet
    Dim re As New RegExp
    
    With re
        .Global = True
        .Pattern = "[N-V]"
    End With
    
    i = 2
    Set sh = Sheets("Listed")
    
    With sh
        Do While Not IsEmpty(.Cells(i, "A"))
            If re.test(.Cells(i, "L")) Then
                .Cells(i, "L") = re.Replace(.Cells(i, "L"), "P")
            End If
            i = i + 1
        Loop
    End With
    
End Sub
 
Upvote 0
The line below causes a compile error: User-defined type not defined. Did I type it wrong?

Code:
Dim re As New RegExp
 
Upvote 0
Code:
Dim oneCell as Range

With Sheet1.Range("A:A")

    For Each oneCell in Range(.Cells(1,1), .Cells(.Rows.Count,1).End(xlup))
        If CStr(oneCell.Value) Like "[N-V]" Then
            oneCell.Value = "P"
        End If
    Next oneCell

End With
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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