+ Reply to Thread
Results 1 to 3 of 3

Excel vba - Insert formula into cell

  1. #1
    Registered User
    Join Date
    11-21-2009
    Location
    Rhyl, North Wales
    MS-Off Ver
    Excel 2003
    Posts
    39

    Excel vba - Insert formula into cell

    Hi there, this is my first ever post so please be kind. I have a workbook within which i have wrote various subs to do range of things. the part i stuck on is getting a sub to insert a formula into a cell, easy enough i hear you say but theres something not quite right. here is an example of the code

    Range("O13").Formula = "=IF(ISBLANK(C12),"",IF(ISERROR(VLOOKUP(C12,INDIRECT("'"&($AG$2-1)&"'!$C:$AG"),12,FALSE)),N12,VLOOKUP(C12,INDIRECT("'"&($AG$2-1)&"'!$C:$AG"),12,FALSE)))


    the problem is when this is entered into vba it turns the "'" after INDIRECT into " '" turning half the formula into a 'note/statement'. I've encountered this problem before and just done a work around but this time unfortunately thats not an option.

    Hope someone can show me the error of my ways, appreciate any help

    Richard Swift
    Last edited by Swift4Play; 11-21-2009 at 10:42 PM.

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Excel vba - Insert formula into cell

    replace all quotes " " with "" ""
    Range("a1").Value = "=IF(ISBLANK(C12),"""",IF(ISERROR(VLOOKUP(C12,INDIRECT("" '""&($AG$2-1)&""'!$C:$AG""),12,FALSE)),N12,VLOOKUP(C12,INDIRECT(""'""&($AG$2-1)&""'!$C:$AG""),12,FALSE)))"


    indirect isnt availiable as a usable worksheet function in vba tho
    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    Last edited by martindwilson; 11-21-2009 at 11:02 PM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    11-21-2009
    Location
    Rhyl, North Wales
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: Excel vba - Insert formula into cell

    You my friend are a star, now thats sorted just hope the rest goes to plan, in the final stages of the workbook, thanks again matey you have my appreciation.

+ 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