+ Reply to Thread
Results 1 to 7 of 7

syntax for If/Else/End If

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    syntax for If/Else/End If

    Keep getting Else Without If errors... someone help me with the proper syntax for my code:

    Private Sub Option1N_Click()
    
    If chk1 = True Then option1Y = True And Range("ErrOpt1N").Value = 0
      
      Else
        
        If chk1 = False Then option1Y = False And Range("ErrOpt1N").Value = 1
      
        End If
    End If
        
      txt4.Value = Range("ErrorScore").Value
    
    End Sub

    Please and thank you!!
    Last edited by HeyInKy; 10-15-2014 at 04:00 PM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: syntax for If/Else/End If

    I am not sure why you have and after then, but this will get rid of the error, though it might not be correct

    Private Sub Option1N_Click()
    
    If chk1 = True Then
       option1Y = True And Range("ErrOpt1N").Value = 0
        
         ElseIf chk1 = False Then option1Y = False And Range("ErrOpt1N").Value = 1
      
        End If
    'End If
        
      txt4.Value = Range("ErrorScore").Value
    
    End Sub

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,536

    Re: syntax for If/Else/End If

    When you put the a statement after the Then on the same line,the whole line stands alone as a single statement. It is not part of an If/End If structure. To fix that, move the statement after the Then onto the next line.

    Because you are checking chk to be either True or False your logic can be further simplified to

    Private Sub Option1N_Click()
    
       If chk1 Then
          option1Y = True And Range("ErrOpt1N").Value = 0
       Else
          option1Y = False And Range("ErrOpt1N").Value = 1
       End If
        
      txt4.Value = Range("ErrorScore").Value
    
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,612

    Re: syntax for If/Else/End If

    Private Sub Option1N_Click()
    
    If chk1 = True Then
        option1Y = True
        Range("ErrOpt1N").Value = 0
    Else
        If chk1 = False Then
            option1Y = False
            Range("ErrOpt1N").Value = 1
        End If
    End If
    txt4.Value = Range("ErrorScore").Value
    
    End Sub

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,536

    Re: syntax for If/Else/End If

    Upon reading AB33's post I realized that you almost certainly have another error.

    option1Y = True And Range("ErrOpt1N").Value = 0
    This is syntactically legal but I am 99.9% sure it's not what you intend.

    I suspect you really want to do two separate things:

    option1Y = True
    Range("ErrOpt1N").Value = 0
    The And is logical operator. It does not mean "do this And do that".

  6. #6
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Re: syntax for If/Else/End If

    Yes - thanks to all - I realized I do not need the AND operator...

    and actually, playing with the provided suggestions, I have two sub routines that appear (at first testing) to accomplish what I need without using ELSE and/or END if...

    Private Sub Option1N_Click()
    
        If chk1 = True Then option1Y = True
    
        If chk1 = False Then Range("ErrOpt1N").Value = 1
        
      txt4.Value = Range("ErrorScore").Value
    
    End Sub
    and

    Private Sub Option1Y_Click()
    
    Range("ErrOpt1N").Value = 0
        
    txt4.Value = Range("ErrorScore").Value
    
    End Sub
    Thanks again!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,612

    Re: syntax for If/Else/End If

    You're welcome.

+ 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. syntax???
    By hvisa in forum Excel General
    Replies: 0
    Last Post: 04-11-2007, 09:14 AM
  2. 'If' syntax
    By hvisa in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-30-2007, 04:33 PM
  3. [SOLVED] help with syntax
    By anny in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2006, 10:10 PM
  4. [SOLVED] Syntax help please
    By GettingThere in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-18-2005, 09:05 PM
  5. Syntax Help
    By Dmorri254 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-02-2005, 11:06 AM

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