+ Reply to Thread
Results 1 to 5 of 5

[SOLVED] Syntax for Writing Long VBA Formula on Multiple Lines

Hybrid View

nwd9s [SOLVED] Syntax for Writing... 06-09-2011, 09:52 AM
shg Re: Syntax for Writing Long... 06-09-2011, 09:55 AM
tarquinious Re: Syntax for Writing Long... 06-09-2011, 10:17 AM
abousetta Re: Syntax for Writing Long... 06-09-2011, 10:28 AM
nwd9s Re: Syntax for Writing Long... 06-13-2011, 06:38 AM
  1. #1
    Registered User
    Join Date
    02-15-2011
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    21

    [SOLVED] Syntax for Writing Long VBA Formula on Multiple Lines

    Hi,

    I have a large formula I am trying to input that is making me put it on two lines. I cannot get the syntax to work properly and was looking for some assistance. The range and actual formula all work properly, I am just looking for how to break the formula onto multiple lines in VBA.

    Range(Cells(4, StartValue), Cells(TimelineRow, TimelineColumn)).Formula = "=IF(ISERROR(VLOOKUP($B4,OFFSET('Future NBV'!$A$3,,,'Macro Workspace'!$S$5,'Macro Workspace'!$S$6),MATCH(C$3,'Future NBV'!$1:$1,0),FALSE)),VLOOKUP($B4,OFFSET('Investment Aircraft'!$H$22,,,'Macro Workspace'!$S$7,'Macro Workspace'!$S$8),MATCH(C$3,'Investment Aircraft'!$21:$21,0)-7,FALSE),IF(ISERROR(VLOOKUP($B4,'Macro Workspace'!$B$5:$K$" & YYY & ",10,FALSE)),VLOOKUP($B4,OFFSET('Future NBV'!$A$3,,,'Macro Workspace'!$S$5,'Macro Workspace'!$S$6),MATCH(C$3,'Future NBV'!$1:$1,0),FALSE),IF(AND(EOMONTH(VLOOKUP($B4,'Macro Workspace'!$B$5:$K$" & YYY & ",10,FALSE),0)>=C$3,VLOOKUP($B4,OFFSET('Future NBV'!$A$3,,,'Macro Workspace'!$S$5,'Macro Workspace'!$S$6),MATCH(C$3,'Future NBV'!$1:$1,0),FALSE)=0),B4-VLOOKUP($B4,'Macro Workspace'!$B$5:$P$" & YYY & ",15,FALSE),IF(AND(C$3>VLOOKUP($B4,'Macro Workspace'!$B$5:$K$" & YYY & ",10,FALSE),VLOOKUP($B4,'Macro Workspace'!$B$5:$L$" & YYY,11,FALSE)="Yes"),0,VLOOKUP($B4,OFFSET('Future NBV'!$A$3,,,'Macro Workspace'!$S$5,'Macro Workspace'!$S$6),MATCH(C$3,'Future NBV'!$1:$1,0),FALSE)))))"
    Many thanks in advance for your help!

    Regards,
    nwd9s
    Last edited by pike; 06-13-2011 at 07:01 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Syntax for Writing Long VBA Formula on Multiple Lines

    When you break a long formula, you have to catenate the sub strings:

    Range("A1").Formula = "=if(this long condition, this expression , " & _
                          "that expression)"
    It's much easier to debug if you assign the formula to a string, and print the string to the Immediate window for perusal.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    05-13-2011
    Location
    New Zealander in London
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: Syntax for Writing Long VBA Formula on Multiple Lines

    Quote Originally Posted by nwd9s View Post
    Hi,

    I have a large formula I am trying to input that is making me put it on two lines.
    To break a long line of code into multiple lines in VBA, you use a space, then the the underscore character (i.e. " _").

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Syntax for Writing Long VBA Formula on Multiple Lines

    Hi tarquinious,

    From what I have tried before the " _" only works for vba code not a formula. What shg suggested is correct. You need to close the first line with a quotation mark, add a space and an & sign then on the new line start with quotation marks.

    abousetta

  5. #5
    Registered User
    Join Date
    02-15-2011
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Syntax for Writing Long VBA Formula on Multiple Lines

    Thanks for your help. I have tested this, and a " & _ needs to be used for splitting a formula in vba.

    Thanks,
    nwd9s

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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