+ Reply to Thread
Results 1 to 8 of 8

How to hardcode a concatenate formula into a cell

  1. #1
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    How to hardcode a concatenate formula into a cell

    Hi, I'm an experienced Excel user, but only getting into the VBA side of things. I want to automate some processes in an old spreadsheet to save time and control data entry. However, I'm stumbling on how to add the concatenate function code. The spreadsheet has rows 1:8 as the "header", so beginning on row 9, in column A, I have the following formula:

    =IF(B9="","",CONCATENATE($A$1," - ",$A$2)) 'if B9 is blank, then leave A9 blank; otherwise, put the "year - month" in A9. Column B is the person's name.

    In my protected spreadsheet, I have prefilled A9:A100 with the above formula because the users tend to either forget to put in the year and month or they use different date formats. At any given time, not all of these rows will have data; thus, the reason for wanting to leave column A blank if there is no name in column B.

    If the user needs to insert a row, then I've created a macro button that will unprotect, insert the row, fill certain cells with predefined data, and protect; however, I can't get the macro to hardcode the concatenate formula into column A. I keep getting various error messages. Obviously, my syntax is wrong.

    Can someone please show me how to do this?

    Thanks!

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,851

    Re: How to hardcode a concatenate formula into a cell

    What does your VBA statement look like? The common problem I see with something like this is the "double quotes" that are a necessary part of the formula text string. You end up needing extra double quotes surrounding the double quotes that you need to be included in the formula string.

    I put a similar formula into a cell while recording a macro. This is the statement it gave me:
    Please Login or Register  to view this content.
    Note the extra double quotes.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: How to hardcode a concatenate formula into a cell

    In A1 style it would look like this.

    Please Login or Register  to view this content.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: How to hardcode a concatenate formula into a cell

    Quote Originally Posted by Murman01 View Post
    however, I can't get the macro to hardcode the concatenate formula into column A. I keep getting various error messages. Obviously, my syntax is wrong.

    Can someone please show me how to do this?
    Which error messages?
    Which line(s) of your code to they relate to?
    What does your code look like?
    Can we see the workbook and the code? Even if in a desensitized version?
    Is it nearly bed time?

    Oh so many questions!

    BSB

  5. #5
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Re: How to hardcode a concatenate formula into a cell

    Thanks everyone for your help! It was the extra double set of quote that was catching me up (now I need to go and research WHY those quotes are necessary!).

    My code that now works as desired is:
    .Range("A" & ActiveCell.Row).Formula = "=If(B" & ActiveCell.Row & "="""","""",CONCATENATE($A$1,"" - "",$A$2))"

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: How to hardcode a concatenate formula into a cell

    WHY those quotes are necessary!).
    The reason is because you are building a string formula to put into a cell.

    When you have "-" you are telling Excel I want - in the cell.

    If you have ""-"" you are telling Excel put what's between the outer quotes in a cell "-", get the idea?

    So you have to double up the quotes otherwise it disregards the quotes as you just saying put what's between these quotes into the string.
    Last edited by skywriter; 01-22-2016 at 05:25 PM.

  7. #7
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Re: How to hardcode a concatenate formula into a cell

    Thanks Skywriter!

  8. #8
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: How to hardcode a concatenate formula into a cell

    Quote Originally Posted by Murman01 View Post
    Thanks Skywriter!
    My pleasure, thanks again for the rep. points.

+ 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] How do I hardcode leading zeros?
    By darkfeld in forum Excel General
    Replies: 6
    Last Post: 01-17-2014, 12:49 PM
  2. Reference Array vs. hardcode
    By abmyers in forum Excel General
    Replies: 3
    Last Post: 07-31-2011, 05:04 PM
  3. Replies: 13
    Last Post: 03-31-2009, 12:21 PM
  4. Hardcode options into list
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-10-2008, 08:28 AM
  5. Hardcode Folder Path
    By mfall in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2008, 12:47 PM
  6. Replies: 1
    Last Post: 10-23-2006, 11:19 AM
  7. [SOLVED] Hardcode Links - edit my code please?
    By Steph in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-26-2005, 12:05 PM
  8. [SOLVED] Change hardcode to ....?
    By Stuart in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2005, 01:06 PM

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