Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

range.formula: text string and cell reference

Latest post Thu, Jun 24 2010 8:14 PM by Rick Williams. 4 replies.
  • Wed, Jun 23 2010 6:18 PM

    • David B
    • Top 50 Contributor
    • Joined on Thu, Aug 14 2008
    • Posts 15
    • Points 219

    range.formula: text string and cell reference

    Hi,

    I'm missing something no doubt very simple...

     

    All I want to do is paste a formula to a cell (within an If statement - different formulas for different cases)

    If typed directly into the cell it would take the form..

    ="ABC "&A1&" DEF"

    so it would result in text string: some number from another cell: second text string

    in VBA I've tried

    Range("A11").Formula = "=ABC" & "&A1&" & " DEF"

    but I just get a #NAME error.  Am I missing extra quotes or something?

    Any help greatfully recieved!

    Cheers

     

     

     

    • Post Points: 21
  • Wed, Jun 23 2010 7:31 PM In reply to

    • Rick Williams
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Melbourne, Australia
    • Posts 76
    • Points 1,189

    Re: range.formula: text string and cell reference

    Ah, this can get tricky when the string you wish to specify contains quotes.

     

    I usually do it in a couple steps:

    1) Start with your original string:  ="ABC "&A1&" DEF"

    2) Replace each quote with two sets of quotes:  =""ABC ""&A1&"" DEF""

    3) Surround your text string with quotes to finish: "=""ABC ""&A1&"" DEF"""

    Viola!

    You need to 'double up' all the quotes, otherwise VBA interprets the single quotes as the end of the text string.

     

    Rick

    Rick Idea
    Melbourne, Australia

    • Post Points: 37
  • Wed, Jun 23 2010 10:43 PM In reply to

    • Omar
    • Top 10 Contributor
    • Joined on Sat, Jan 12 2008
    • Kitchener, ON
    • Posts 128
    • Points 1,823

    Re: range.formula: text string and cell reference

    Rick, that's a nifty way to approach that. I might actually be able to make such a procedure work!

    Omar Freeman Kitchener, ON

    • Post Points: 5
  • Thu, Jun 24 2010 11:36 AM In reply to

    • David B
    • Top 50 Contributor
    • Joined on Thu, Aug 14 2008
    • Posts 15
    • Points 219

    Re: range.formula: text string and cell reference

    Thanks for the replies.

    This approach works! Thanks

    I now have another problem.... not sure if it should go here or in another thread...

    • Post Points: 21
  • Thu, Jun 24 2010 8:14 PM In reply to

    • Rick Williams
    • Top 25 Contributor
    • Joined on Wed, Jan 9 2008
    • Melbourne, Australia
    • Posts 76
    • Points 1,189

    Re: range.formula: text string and cell reference

    You did right starting another thread - I think it helps keep things organised, and allows you to mark this one as closed/answered (as I see you already have). ;-)

    Rick Idea
    Melbourne, Australia

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright Excel User Group and the relevant contributors, 2010. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.