+ Reply to Thread
Results 1 to 5 of 5

Syntax error for If then Else formula

  1. #1
    Registered User
    Join Date
    07-25-2017
    Location
    Latvia
    MS-Off Ver
    MS Office 2010
    Posts
    91

    Syntax error for If then Else formula

    Hi! I'm begginner in VBA, so this question might llok dumb for some of you.

    I have data in A2:C17.

    For D2:D17 I need following formula:

    D2=IF(B2>=15;A2+B2;B2).

    My VBA code is this:

    Sub Rectangle1_Click()
    Dim x As Integer
    For x = 2 To 17
    If Cells(x, 2).Value >= 15
    Then
    Cells(x, 4).Value = Cells(x, 2) + Cells(x, 1)
    Else
    Cells(x, 4).Value = Cells(x, 2)
    End If
    Next x
    End Sub

    It shows Compile Error: Syntax Error and colors this line red:
    If Cells(x, 2).Value >= 15
    Then

    What I am doing wrong?
    Attached Files Attached Files

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,891

    Re: Syntax error for If then Else formula

    "Then" should be in same line as If statement.
    Please Login or Register  to view this content.
    FYI - Please nest your code in code tag (# button).
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,093

    Re: Syntax error for If then Else formula

    The word "Then" should go at the end of that line, not on the next line.

  4. #4
    Registered User
    Join Date
    07-25-2017
    Location
    Latvia
    MS-Off Ver
    MS Office 2010
    Posts
    91

    Re: Syntax error for If then Else formula

    Thanks. Did not know it mattered.

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,093

    Re: Syntax error for If then Else formula

    You're welcome & thanks for the feedback

+ 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] syntax of array vba formula-error
    By pnnaik in forum Excel General
    Replies: 3
    Last Post: 04-01-2018, 01:55 PM
  2. Syntax Error in vlookup(hlookup Formula
    By ExcelFinWizzard in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-15-2014, 07:05 PM
  3. [SOLVED] ActiveCell.Formula Syntax Error
    By Leipzig in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2013, 12:18 PM
  4. [SOLVED] If-Then nested formula not working...must be a syntax error
    By sunny_island2002 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-16-2012, 02:37 PM
  5. Complex Formula Syntax error
    By Cyd44 in forum Excel General
    Replies: 2
    Last Post: 07-01-2012, 10:35 AM
  6. [SOLVED] Formula syntax error - chinese and gibberish
    By Joshua Fandango in forum Excel General
    Replies: 3
    Last Post: 03-29-2005, 09:06 AM
  7. Error with formula syntax
    By menefist in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2005, 03:42 PM

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