+ Reply to Thread
Results 1 to 4 of 4

Thread: If Statment

  1. #1
    Registered User
    Join Date
    11-10-2009
    Location
    Sunnyvale, Ca
    MS-Off Ver
    Excel 2003
    Posts
    7

    If Statment

    Hi,

    I would like to know how to write if statements that includes "wildcards" for spaces. For example:

    Code:
     
    If rs!Revision <> Me.txtRev Then
      ' do something here
    End if
    my problem is that if rs!Revision = "L" and Me.txtRev = "L " then the program would think that those two are not alike because of the space after the "L " in Me.txtRev and therefore it goes though the loop when I don't want it to.

    Is there a way to have it something like this (I tried the code below and i know it doesn't work):

    Code:
    If rs!Revision <> " * & me.txtRev & * " then 
      ' do something here
    End if
    There is something wrong with this part of the code: " * & me.txtRev & * " and I don't know how to fix it.

    Please help.

    Thanks!
    Last edited by tnguyen; 12-02-2009 at 05:12 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007
    Posts
    6,259

    Re: If Statment

    Check out "Like" in VBA help
    Dave


  3. #3
    Registered User
    Join Date
    03-20-2008
    Location
    Buffalo, NY USA
    Posts
    43

    Re: If Statment

    You're close with the wild cards - it's your quotes that are throwing it off. And the use of the Like operator is correct too.

    Code:
    'This will find '*L*' (L anywhere in me.txtRev)
    If rs!Revision Like "*" & me.txtRev & "*" then 
      ' do something here
    End if
    
    'This will find 'L*' (L at the start of me.txtRev)
    If rs!Revision Like me.txtRev & "*" then 
      ' do something here
    End if
    
    'This will find '*L' (L at the end of me.txtRev)
    If rs!Revision Like "*" & me.txtRev then 
      ' do something here
    End if

  4. #4
    Registered User
    Join Date
    11-10-2009
    Location
    Sunnyvale, Ca
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: If Statment

    Thanks! That works.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0