+ Reply to Thread
Results 1 to 12 of 12

Macro to concatenate text strings

Hybrid View

zcheema Macro to concatenate text... 04-23-2012, 12:41 PM
arlu1201 Re: Macro to concatenate text... 04-23-2012, 02:08 PM
zcheema Re: Macro to concatenate text... 04-23-2012, 02:18 PM
shg Re: Macro to concatenate text... 04-23-2012, 02:24 PM
zcheema Re: Macro to concatenate text... 04-23-2012, 02:29 PM
zcheema Re: Macro to concatenate text... 04-23-2012, 02:30 PM
shg Re: Macro to concatenate text... 04-23-2012, 02:32 PM
zcheema Re: Macro to concatenate text... 04-23-2012, 02:41 PM
shg Re: Macro to concatenate text... 04-23-2012, 02:44 PM
zcheema Re: Macro to concatenate text... 04-23-2012, 02:54 PM
shg Re: Macro to concatenate text... 04-23-2012, 02:55 PM
zcheema Re: Macro to concatenate text... 04-23-2012, 03:12 PM
  1. #1
    Registered User
    Join Date
    04-23-2012
    Location
    Toronto, Ontario
    MS-Off Ver
    2003
    Posts
    18

    Unhappy Macro to concatenate text strings

    Hi - I am trying to concatatenate text strings from one sheet to another. The raw data sheet consists of Names of people and Comments made by each person.
    I want to concatenate each comment made by each person and produce the results in a new sheet. My list of names and their comments would be a dynamic list - that is - each time a person enters a new comment - a new row will be created with the persons name and his comment.
    my raw data would be:

    Find the example in the attached image..
    pic.JPG

    Note that in the summary tab - any "(blank)" comments will be ignored..and only the non blanks will be picked..the names in the summary tab are pre-populated i.e. they are standard names and will not be dynamic. its a fixed list of names. only Tom, Harry, PEter and Rob will be on this list. On an on going basis - their comments will be updated based on what we see in Sheet1(raw data). We do not need to re-generate these names from the first sheet. i will do that already.

    The macro will run through Sheet1 (raw data) and will look look at each comment the person has made and then concatenate it into sheet 2 - where the names of the person already exsist. the macro will only output results in "Comments" column of the Sheet 2!

    Can you please help me create something of this sort??

    I am using excel 2003
    Last edited by zcheema; 04-23-2012 at 12:47 PM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to concatenate text strings

    Please attach an excel file instead of a jpg. It makes it much easier for us to provide you a solution.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    04-23-2012
    Location
    Toronto, Ontario
    MS-Off Ver
    2003
    Posts
    18

    Re: Macro to concatenate text strings

    Here is the attachment... Thanks!!
    Attached Files Attached Files

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to concatenate text strings

    Do you know how to use a user-defined function?
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    04-23-2012
    Location
    Toronto, Ontario
    MS-Off Ver
    2003
    Posts
    18

    Re: Macro to concatenate text strings

    i tried to create one..i am working on it actually but its a longer approach..
    i created this function:
    Function ConcatRange(CellBlock As Range) As String
    Dim Cell As Range
    Dim sbuf As String
    For Each Cell In CellBlock
    If Len(Cell.Text) > 0 Then sbuf = sbuf & Cell.Text & " ; "
    Next
    ConcatRange = Left(sbuf, Len(sbuf) - 1)
    
    end function

    i was thinking of first creating columns of each name in the Raw sheet i.e. append columns Tom, Harry, etc..
    and first copy their respective comments under their columns..that means in Toms column - i will copy toms comment if its non blank..

    Name comment Tom Harry
    Tom 123 123
    harry xcbv xcbv
    peter
    tom 332 332


    and then i could call the concatrange function that i have created. it works fine but i see extra delimiters i.e. ";" after each comment. if i could remove these for non blank cells then i could concatenate them all..
    does this make sense? i know its a longer approach!!! i coudlnt figure out a decent macro so tried the function instead..
    Last edited by arlu1201; 04-23-2012 at 02:42 PM. Reason: Use code tags in future.

  6. #6
    Registered User
    Join Date
    04-23-2012
    Location
    Toronto, Ontario
    MS-Off Ver
    2003
    Posts
    18

    Re: Macro to concatenate text strings

    i am fairly new to macros and functions so my approach could be completely off !

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to concatenate text strings

          ---A---- ----------B---------- C ---D--- -------------------E-------------------
      1   Raw Data                         Summary                                        
      2   Name     Comments                Name    Comments                               
      3   Tom      Amazing!!               Peter   I don't know anything                  
      4   Harry    I knew it..             Harry   I knew it..                            
      5   Peter                            Rob                                            
      6   Tom      Where are you?          Tom     Amazing!!; Where are you?; I am outside
      7   Tom      I am outside                                                           
      8   Peter    I don't know anything                                                  
      9   Rob
    The formula in E2 and copied down, which MUST be confirmed with Ctrl+Shift+Enter rather than just Enter, is

    =CatIf(($A$3:$A$9=D3)*($B$3:$B$9<>""), $B$3:$B$9, "; ")

    Function CatIf(avbIf As Variant, _
                   rInp As Range, _
                   Optional sSep As String = ",") As String
        ' shg 2007
        ' Catenates the elements of vInp separated by sSep
    
        Dim iRow        As Long
        Dim iCol        As Long
        Dim i           As Long
    
        On Error Resume Next
        i = UBound(avbIf, 2)
    
        If Err.Number Then
            ' avbIf is 1D
            For iRow = 1 To rInp.Rows.Count
                For iCol = 1 To rInp.Columns.Count
                i = i + 1
                    If avbIf(i) Then CatIf = CatIf & rInp(iRow, iCol) & sSep
                Next iCol
            Next iRow
        Else
            ' it's 2D
            For iRow = 1 To rInp.Rows.Count
                For iCol = 1 To rInp.Columns.Count
                    If avbIf(iRow, iCol) Then CatIf = CatIf & rInp(iRow, iCol) & sSep
                Next iCol
            Next iRow
        End If
        
        If Len(CatIf) Then CatIf = Left(CatIf, Len(CatIf) - Len(sSep))
    End Function

  8. #8
    Registered User
    Join Date
    04-23-2012
    Location
    Toronto, Ontario
    MS-Off Ver
    2003
    Posts
    18

    Re: Macro to concatenate text strings

    shg thanks for your prompt help ! although i had a quick concern - the =catif function has a range in them. my raw data is actually structured like this..
    there are a bunch of other columns after name - so the first column is names..and the last column is comments..
    will the catif function still work??
    name column1 column 2 .....column 10...............comments

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to concatenate text strings

    Just reference the ranges you need.

  10. #10
    Registered User
    Join Date
    04-23-2012
    Location
    Toronto, Ontario
    MS-Off Ver
    2003
    Posts
    18

    Re: Macro to concatenate text strings

    wow! this works like a charm - although i had one more question..
    the raw data rows are not static i.e. with time. more rows will be added. if Tom has a new comment he will go into raw data and will insert another row with his name and comment.
    with the above function - i will have to manualyl go into the summary sheet and then update the formula to grab toms new comment as well??

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to concatenate text strings

    Extend the ranges further down than you need.

  12. #12
    Registered User
    Join Date
    04-23-2012
    Location
    Toronto, Ontario
    MS-Off Ver
    2003
    Posts
    18

    Re: Macro to concatenate text strings

    ok PerfecT! got it
    can you pelase please help me with one other thing as well? in the same workbook am also plugging in question scores from each person and then taking an avegare of them.
    in the attached file you will see that in the extra columns in beteween am actually capturing the scores from each individual. once i get these scores per question - i take an avergae of them across per question as well as across each individual.

    the score values will only be 0, 1,2,3,4,N/A
    if the score is 0 i will count it in my sum and in my average as well.
    if score value is N/A - i will not count this in my sum or my average

    i have pasted the summary below the raw data - cuz that is how my final chart will look like. in the first columns i will have the data. and at the end of the same worksheet or a new worksheet - i will have a summarized view which will have a concatenation of all comments per person and an average of their scores
    Concatenate Help.xls

+ 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