If(isnumber(search) vba

rakesh seebaruth

Active Member
Joined
Oct 6, 2011
Messages
303
HI Guys

i have the following table

I
L S
Mr John Smith
Mr Jack Paul and Mrs Jack Paul


my vba codes are as follows:-

Sub x()
Dim TR As Long
TR = Cells(Rows.Count, "I").End(xlUp).Row
Range("L2:L" & TR) = Evaluate("IF(ISNUMBER(SEARCH(""MR"",I2:I" & TR & ")),""Dear Sir"","""")")
Dim SS As Long
SS = Cells(Rows.Count, "L").End(xlUp).Row
Range("S2:S" & SS) = Evaluate("IF(ISNUMBER(SEARCH(""Dear Sir"",L2:L" & SS & ")),""your banking facility"","""")")
End Sub


What i want is

if in column I there are Mr and Mrs then column L= Dear Sir/Madam. and if L= Dear Sir/Madam then Column S= your banking facilities.
With Mr its works well.

Thanks for your help

regards

rakesh
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This might suit:

Code:
Sub Revised()
    
    Dim TR As Long
    Dim rngCell As Range
    Dim bMR As Boolean
    Dim bMRS As Boolean
    
    TR = Cells(Rows.Count, "I").End(xlUp).Row
    
    For Each rngCell In Range("I1:I" & TR)
        bMR = False: bMRS = False
        Debug.Print rngCell.Value
        If InStr(UCase(rngCell.Value), "MR ") > 0 Then bMR = True
        If InStr(UCase(rngCell.Value), "MR.") > 0 Then bMR = True
        If InStr(UCase(rngCell.Value), "MRS ") > 0 Then bMRS = True
        If InStr(UCase(rngCell.Value), "MRS.") > 0 Then bMRS = True
        
        If bMR And bMRS Then
            rngCell.Offset(0, 3).Value = "Dear Sir/Madam"
            rngCell.Offset(0, 10).Value = "your banking facilities"
        ElseIf bMR Then
            rngCell.Offset(0, 3).Value = "Dear Sir"
            rngCell.Offset(0, 10).Value = "your banking facility"
        ElseIf bMRS Then
            rngCell.Offset(0, 3).Value = "Dear Madam"
            rngCell.Offset(0, 10).Value = "your banking facility"
        End If
    Next
End Sub
 
Upvote 0
This is sooo complicated...
You have no reason to calculate UCase(rngCell.Value) and rngCell.Offset(0, 3) many times.
You have no reason to use the variant Value property and variant UCase function when you need a string.
Write simply

Code:
Const er = "A creature with no sex found!" & vbLf & "Row: "
Dim rngCell As Range, ucStr$, i&
For Each rngCell In [I1].Resize(Cells(Rows.Count, "I").End(xlUp).Row)
    With rngCell
        ucStr=UCase$(.Text)
[LEFT]        i = Sgn(InStr(ucStr, "MR ") + InStr(ucStr, "MR.")) * 2 + Sgn(InStr(ucStr, "MRS"))[/LEFT]
        If i Then
            .Offset(, 3) = Choose(i, "Dear Madam", "Dear Sir", "Dear Madam/Sir")
[LEFT][COLOR=#333333][FONT=monospace][LEFT][COLOR=#222222][FONT=Verdana]            .Offset(, 10) = "your banking facilit" [COLOR=#222222][FONT=Verdana]&[/FONT][/COLOR] IIf(i = 3, "ies", "y")
        Else
            MsgBox er [COLOR=#222222][FONT=Verdana]&[/FONT][/COLOR] .Row, , "Error dude"
        End If
    End With
Next
[/FONT][/COLOR][/LEFT]
[/FONT][/COLOR][/LEFT]

(It's not my fault, it's a fault of the idiot who programmed this editor that is not able to keep formatting and presents a dud as my work.)
 
Upvote 0
Again, Value is faster than Text.
 
Upvote 0
@Jan Mach - Thanks for the optimization tips. I had not known of/remembered the SGN function.

@RoryA - Is there a master list somewhere that shows relative speed of Excel functions? (Both for VBA and Formulas) Google does not bring up anything organized that I could locate.
 
Last edited:
Upvote 0
No, not that I'm aware of.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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