Consulting

Results 1 to 6 of 6

Thread: Cell Contains Word

  1. #1
    VBAX Regular
    Joined
    May 2008
    Posts
    7
    Location

    Cell Contains Word

    Hello,

    I am running a script to hide rows if it equals a certian test:

    Sub hideit()
    For n = 3 To 10
    If Cells(n, "B") = "Eng" Then
    Cells(n, "B").EntireRow.Hidden = True
    Else
    Cells(n, "B").EntireRow.Hidden = False
    End If
    Next n
    End Sub

    So if it equals "Eng" then hide the row. I also have certian cells that contain more then one word like "Eng 1", "Eng 2", etc. How do you tell it to hide if the string contains "Eng" not just equals? I have tried searching but unable to find a solution.

    Tnx

    Andrew

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,211
    Location
    Try this...

    [VBA]Sub hideit()
    For n = 3 To 10
    If InStr(Cells(n, "B"), "Eng") Then
    Cells(n, "B").EntireRow.Hidden = True
    Else
    Cells(n, "B").EntireRow.Hidden = False
    End If
    Next n
    End Sub[/VBA]

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2404, Build 17531.20128

  3. #3
    VBAX Regular
    Joined
    May 2008
    Posts
    7
    Location
    That worked like a charm and yet so simple .

    Thanks

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Constructs like [VBA]If condition then
    Variable = True
    Else
    Variable = False
    End if[/VBA]can be replaced with[VBA]Variable = condition
    [/VBA]
    As in
    [VBA]Sub hideit()
    For n = 3 To 10
    Cells(n, "B").EntireRow.Hidden = CBool(InStr(Cells(n, "B"), "Eng"))
    Next n
    End Sub [/VBA]

  5. #5
    VBAX Regular
    Joined
    May 2008
    Posts
    7
    Location
    Quote Originally Posted by mikerickson
    Constructs like [vba]If condition then
    Variable = True
    Else
    Variable = False
    End if[/vba]can be replaced with[vba]Variable = condition
    [/vba] As in
    [vba]Sub hideit()
    For n = 3 To 10
    Cells(n, "B").EntireRow.Hidden = CBool(InStr(Cells(n, "B"), "Eng"))
    Next n
    End Sub [/vba]
    Cool, tnx

    Will this run faster then a Case statement?

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Select Case is one of the slower branching options.
    With this construction
    If Condition then
       var = True
    Else
       var = False
    End If
    VBA has to
    1) calculate Condition
    2) test if Condition = True
    3) goto the appropriate instruction
    4) assign var a value

    With[VBA]var = Condition[/VBA]it has to
    1) calculate Condition
    2) assign var that value

    If one can avoid branching ("If" in all of its manifestations, including IIf and constructions like CLng(a=2)) the routine will be faster.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •