+ Reply to Thread
Results 1 to 8 of 8

Problem with If Or statement.

Hybrid View

kjg Problem with If Or statement. 02-06-2019, 05:12 AM
KOKOSEK Re: Problem with If Or... 02-06-2019, 05:29 AM
kjg Re: Problem with If Or... 02-06-2019, 06:22 AM
Akuini Re: Problem with If Or... 02-06-2019, 05:48 AM
kjg Re: Problem with If Or... 02-06-2019, 06:18 AM
banaanas Re: Problem with If Or... 02-06-2019, 06:21 AM
KOKOSEK Re: Problem with If Or... 02-06-2019, 06:31 AM
kjg Re: Problem with If Or... 02-06-2019, 06:39 AM
  1. #1
    Forum Contributor
    Join Date
    11-30-2005
    Location
    Penzance Cornwall, UK
    MS-Off Ver
    Office 365
    Posts
    205

    Problem with If Or statement.

    Hi Guys,

    I am trying to use an If Or statement but seem to be having a problem.

    There could be a filenamed TEST or it could be Test.

    I need a message box to tell the user if the ActiveWorkbook.Name is NOT TEST or Test

    This works if the file is named Test.xlsm.
    If ActiveWorkbook.Name <> "Test.xlsm" Then
    This works if the file is called TEST.xlsm.
    If  ActiveWorkbook.Name <> "TEST.xlsm" Then
    This does NOT work, always shows the message box, even if the active workbook is either of the filenames.

    If ActiveWorkbook.Name <> "Test.xlsm" OR ActiveWorkbook.Name <> "TEST.xlsm" Then
    MsgBox "The workbook is NOT called TEST or Test", vbCritical, Title:="Error"
    Exit Sub
    End If

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: Problem with If Or statement.

    How about:

    If (UCase(ActiveWorkbook.Name) <> "TEST") Then
      MsgBox "The workbook is NOT called TEST or Test", vbCritical, Title:="Error"
      Exit Sub
    End If
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Forum Contributor
    Join Date
    11-30-2005
    Location
    Penzance Cornwall, UK
    MS-Off Ver
    Office 365
    Posts
    205

    Re: Problem with If Or statement.

    I tried (UCase(ActiveWorkbook.Name) <> "TEST") and (UCase(ActiveWorkbook.Name) <> "Test") just in case, but both failed.

    I replaced the OR with AND as suggested by Akuini, the AND worked great.

    Thanks for trying Kokosek.

  4. #4
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Problem with If Or statement.

    Try:
    If ActiveWorkbook.Name <> "Test.xlsm" AND ActiveWorkbook.Name <> "TEST.xlsm" Then

  5. #5
    Forum Contributor
    Join Date
    11-30-2005
    Location
    Penzance Cornwall, UK
    MS-Off Ver
    Office 365
    Posts
    205

    Re: Problem with If Or statement.

    I have just tested the AND instead of the OR and it works great, thanks Akuini.

  6. #6
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: Problem with If Or statement.

    Personally I find that a good practice always when you do string comparisons is to use (ucase(trim("String here")) = (ucase(trim("String2 Here")) to trim excess spaces and make it uppercase when doing comparisons.
    As KOKOSEK already posted

  7. #7
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: Problem with If Or statement.

    Does not make sense because Ucase("test") = Ucase("TEST") so there is no need to check twice with AND.
    Why on my excel in ActiveWorkbook.Name I got only Book1 without .xlsm?

    p.s. I am dumb. On fresh sheet before 1st save, there is no extension yet.

    Anyhow:
    Lower case sort this out in one check:
    If (LCase(ActiveWorkbook.Name) <> "test.xlsm") Then
      MsgBox "The workbook is NOT called TEST or Test", vbCritical, Title:="Error"
      Exit Sub
    End If
    Last edited by KOKOSEK; 02-06-2019 at 08:18 AM.

  8. #8
    Forum Contributor
    Join Date
    11-30-2005
    Location
    Penzance Cornwall, UK
    MS-Off Ver
    Office 365
    Posts
    205

    Re: Problem with If Or statement.

    I don't know, when stepping through my code, I have inserted Debug.Print this shows the full name including the .xlsm as does MsgBox ActiveWorkbook.Name

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] If statement Problem
    By excobra in forum Excel General
    Replies: 6
    Last Post: 08-24-2016, 06:09 AM
  2. [SOLVED] If - Then - Else Statement Problem: Next without For
    By brgr4u in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-20-2013, 02:04 PM
  3. [SOLVED] IF Statement PROBLEM
    By Umar in forum Excel General
    Replies: 2
    Last Post: 06-14-2012, 07:20 AM
  4. Now() statement within a IF(AND statement problem
    By Modify_inc in forum Excel General
    Replies: 3
    Last Post: 03-27-2011, 04:17 PM
  5. Problem with IF statement within a COUNTIF statement
    By girlofscience in forum Excel General
    Replies: 5
    Last Post: 04-22-2009, 10:20 AM
  6. [SOLVED] IF Statement Problem
    By RagDyer in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 09-06-2005, 06:05 AM
  7. Re: If Statement Problem
    By Robert Hargreaves in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2005, 09:05 PM

Tags for this Thread

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.6.0 RC 1