+ Reply to Thread
Results 1 to 5 of 5

VBA Automatically concatenate cell data based on data, and prefix with specific word.

  1. #1
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    VBA Automatically concatenate cell data based on data, and prefix with specific word.

    Hi,

    Could anyone help me with what may be a simple solution using VBA please? What I am trying to do is to take all values from (for example) cells A2 to G2 and concatenate those values into cell H2, seperated by a comma, or to leave a comma out (skip it) if no data exists in any of those cells:

    For example if all cells have data:
    A2 B2 C2 D2 E2 F2 G2 H2
    I Am A Real Piece Of Work I, Am, A, Real, Piece, Of, Work

    For example if NOT all cells have data:
    A2 B2 C2 D2 E2 F2 G2 H2
    I Am A Piece Work I, Am, A, Piece, Work

    But I would like to do this on the worksheet_change function so that it updates H2 each time it leaves one of the cells in the row, data or not.

    I have an additional requirement that if cell A2 contains a specific word/number, the finished concatenation in cell H2 is prefixed with a certain word, i.e. "Hi", so that the result would be:

    Hi, I, Am, A, Real, Piece, Of, Work

    Any help would be greatly appreciated, I have searched all over this board to find a result, but none that work with the worksheet_change function, or at least without having to call a macro each time.

    Thanks in advance!
    Jamie
    Last edited by jamieswift1977; 01-30-2017 at 12:03 PM.

  2. #2
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    469

    Re: VBA Automatically concatenate cell data based on data, and prefix with specific word.

    Please Login or Register  to view this content.
    And then just use the function in the cell you would like it in. Alsom change the "/" to ",".
    Last edited by DannyJ; 01-30-2017 at 12:06 PM.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Automatically concatenate cell data based on data, and prefix with specific word.

    Here's one way

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA Automatically concatenate cell data based on data, and prefix with specific word.

    Thanks very much DannyJ and Richard, that's very helpful! I've gone with Richard's idea as it had the worksheet_change function, but thanks for your help anyway!

    Richard, is there any way of implementing the "Hi" prefix in cell A if it contains a value? Otherwise the code is perfect! Thanks!

  5. #5
    Registered User
    Join Date
    08-30-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    37

    Re: VBA Automatically concatenate cell data based on data, and prefix with specific word.

    Me again Richard, sorry after testing it all, the code seemed to work fine until when all data in cells (A2 to G2) was removed, and it came up with this error:

    Run-time error '5':
    Invalid procedure call or argument

    Is there any way to eliminate this error please?
    Last edited by jamieswift1977; 01-30-2017 at 02:30 PM.

+ 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. Concatenate Cell Value by specific range and specific data
    By ivhysenbelli in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-16-2016, 12:04 PM
  2. [SOLVED] Add a prefix word to cells using Concatenate method ... but not working for me..
    By PeteMeehan in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-09-2016, 08:38 PM
  3. How to get and automatically updated monthly average based on data from specific dates.
    By daveneedshelp123 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-04-2013, 03:41 AM
  4. Really easy task - automatically entering data based upon matches in a specific column
    By isthisthewaywego in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-13-2013, 09:00 AM
  5. [SOLVED] Automatically copy data from one worksheet to another based on a specific word entered
    By arkadd61 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2013, 08:23 PM
  6. [SOLVED] Concatenate based on cell data
    By Healthport - Jamie in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-13-2012, 03:04 PM
  7. [SOLVED] Automatically create graphs based on specific data
    By Tino XXL in forum Excel General
    Replies: 2
    Last Post: 01-27-2012, 04:48 AM

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