?
Solved

Phrase Not Like won't work in VBA If statement

Posted on 2011-04-20
8
Medium Priority
?
435 Views
Last Modified: 2012-06-27
I have a field on an Excel 2003 form that needs to be checked to be sure the user entered, as part of the code name, the terms either COUNTRY or CURRENCY.  However, I can't seem to get the If statement to evaluate the Not Like phrase.
'First check to be sure Portfolio Code has the term of either COUNTRY or CURRENCY in it.
If Me.txtPortfolioCode.text Not Like "*COUNTRY*" Or Me.txtPortfolioCode.text  Not Like "*CURRENCY*" Then
    MsgBox "You have not indicated whether this is a COUNTRY or CURRENCY " & vbCrLf & _
                  "for the Worksheet Tab Name.  Please correct naming convention and then save data.", vbOKOnly
                  Me.txtPortfolioCode.SetFocus
                  Exit Sub
    End If

Open in new window

0
Comment
Question by:ssmith94015
  • 4
  • 3
8 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35433684
If Not Me.txtPortfolioCode.text Like "*COUNTRY*" And Not Me.txtPortfolioCode.text  Like "*CURRENCY*" Then

Open in new window


for example.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 35433688
If Not Me.txtPortfolioCode.text Like "*COUNTRY*" Or Not Me.txtPortfolioCode.text Like "*CURRENCY*" Then

Kevin
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 35433698
Note: And rather than Or. :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ssmith94015
ID: 35433712
Change to below and still does not recognize the phrase.  I tried using both text and value

If Not Me.txtPortfolioCode.Value Like "*COUNTRY*" Or Not Me.txtPortfolioCode.text Like "*CURRENCY*" Then
    MsgBox "You have not indicated whether this is a COUNTRY or CURRENCY " & vbCrLf & _
                  "for the Worksheet Tab Name.  Please correct naming convention and then save data.", vbOKOnly
                  Me.txtPortfolioCode.SetFocus
                  Exit Sub
    End If

If Not Me.txtPortfolioCode.text Like "*COUNTRY*" Or Not Me.txtPortfolioCode.text Like "*CURRENCY*" Then
    MsgBox "You have not indicated whether this is a COUNTRY or CURRENCY " & vbCrLf & _
                  "for the Worksheet Tab Name.  Please correct naming convention and then save data.", vbOKOnly
                  Me.txtPortfolioCode.SetFocus
                  Exit Sub
    End If
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35433740
I repeat: And not Or, as I put in my code. ;)
0
 

Author Closing Comment

by:ssmith94015
ID: 35433749
That was it, using AND rather than OR.  Thank you.

Sandra
0
 

Author Comment

by:ssmith94015
ID: 35433770
Sorry rorya, I did not notice the difference at first so missed the AND usage.  Also, my screen did not refresh so missed your added comments referencing this oversight.  Thank you again for the advice.

Sandra
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35433786
No worries. Glad to help. :)
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question