Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Problem using 'IF' in a macro

Latest post Thu, Jul 3 2008 8:00 AM by Nick Hodge. 3 replies.
  • Wed, Jul 2 2008 9:38 AM

    • xania
    • Top 150 Contributor
    • Joined on Wed, Feb 27 2008
    • Posts 2
    • Points 42

    Confused [*-)] Problem using 'IF' in a macro

    I am having trouble with an 'IF' instruction within a Macro.  My system at work is Windows XP Pro SP2 and Office 2003 SP3.

    I have written the following macro to check whether 2 cells have been updated and to report if either have not but to switch to a new spreadsheet if both have been changed:

    Sub Order()
    '
    ' Order Macro
    ' Macro recorded 23/06/2008 by Stmcar
    '
        If A1 = 0 Then MsgBox "WARNING - you have not provided a teacher name - cannot proceed" Else _
        If A2 = 0 Then MsgBox "WARNING - you have not provided a Course - cannot proceed" Else _
        Sheets("Order").Select
      
     
    End Sub

    No matter what value I have in A1 or B1, the macro reads the contents of these fields as '0' so that as it stands I always get the message "WARNING - you have not provided a teacher name - cannot proceed"

    but if I modify the first line to

    If A1 <> 0 Then MsgBox "WARNING - you have not provided a teacher name - cannot proceed" Else _

    I then get the message "WARNING - you have not provided a Course - cannot proceed".

    I have tried modifications like using cell naming, using alha characters, and cell comparisons i.e.

    If Namecheck = 0 Then MsgBox "WARNING etc or If A1 = B1 Then MsgBox "WARNING

    but it just seems as it the 'IF' part is always returning 'True' whatever I do.

    • Post Points: 21
  • Wed, Jul 2 2008 1:40 PM In reply to

    Re: Problem using 'IF' in a macro

    That is because your code is referring to a variable called A1, not cells

    Use

    Sub Order()
    '
    ' Order Macro
    ' Macro recorded 23/06/2008 by Stmcar
    '
        With Worksheets("Sheet1")
       
            If .Range("A1").Value = 0 Then
           
                MsgBox "WARNING - you have not provided a teacher name - cannot proceed"
               
            Else If .Range("A2").Value = 0 Then
                MsgBox "WARNING - you have not provided a Course - cannot proceed"
               
            Else
                Sheets("Order").Select
              End If
          End With
     
    End Sub

    Change Sheet1 to the appropriate sheet being checked

    Regards

    Bob

    • Post Points: 5
  • Thu, Jul 3 2008 3:38 AM In reply to

    • xania
    • Top 150 Contributor
    • Joined on Wed, Feb 27 2008
    • Posts 2
    • Points 42

    Re: Problem using 'IF' in a macro

     I'm still learning and tend to forget the basics sometimes.  Thanks very much - I knew I'd missed something simple.Embarrassed

    • Post Points: 21
  • Thu, Jul 3 2008 8:00 AM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 242
    • Points 4,033
    • MVP

    Re: Problem using 'IF' in a macro

     Xania

    Adding to Bob's reply, if you enter

    Option Explicit

    At the top of each module, you will be forced to declare all variable (or get an error). This may have guided you in this one.

    You can force the VBE to add this automatically by setting 'Require variable declaration' under Tools>options in the VBE

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under:
    • Post Points: 5
Page 1 of 1 (4 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.