+ Reply to Thread
Results 1 to 4 of 4

How to code an IF formula in VBA with text results and display actual formula in cell

  1. #1
    Registered User
    Join Date
    06-14-2013
    Location
    Luxembourg
    MS-Off Ver
    Excel 2007
    Posts
    7

    Question How to code an IF formula in VBA with text results and display actual formula in cell

    Hi all,

    I wonder if anyone could shed any light on the below.

    I would like to construct an IF formula in VBA, so that the formula is displayed in the cell itself and not just the result. Certain results in the IF formula are text and Excel requires that they are enclosed in "". The probelm is, when my formula is displayed on the worksheet, the text responses are not enclosed in "", resulting in a #NAME? error. This will be easier to understand with an example.

    My code is:

    Please Login or Register  to view this content.
    In the cell on the worksheet this results in:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    IN, OK, Diff = and N/A need to be enclosed in "".

    What I am after, therefore, is:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I have tried placing extra "" around the text I want as my result, but this does not work.

    Thanks in advance,

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: How to code an IF formula in VBA with text results and display actual formula in cell

    Wouldn't this do it (untested):

    Please Login or Register  to view this content.
    Please click the * below if this helps

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How to code an IF formula in VBA with text results and display actual formula in cell

    In VBA, your formula is a text string surrounded by quotes. To define a literal quote within a text string, use two consecutive quotes for each literal quote you want to define.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    06-14-2013
    Location
    Luxembourg
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How to code an IF formula in VBA with text results and display actual formula in cell

    Thanks for your suggestion JasperD. Unfortunately, this results in a run time error '1004' Application-defined or Object-defined error.

    Quote Originally Posted by JasperD View Post
    Wouldn't this do it (untested):

    Please Login or Register  to view this content.
    AlphaFrog, your solution worked. I figured it would be something like this, but I hadn't bee able to get it to work successfully. Thinking about it, where I was going wrong is that I was splitting the VBA code into several text strings instead of one text string.

    In VBA, your formula is a text string surrounded by quotes. To define a literal quote within a text string, use two consecutive quotes for each literal quote you want to define.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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