+ Reply to Thread
Results 1 to 15 of 15

VBA Code for concatenating text from multiple (dynamic no of) worksheets

  1. #1
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Hi all,

    I am looking for a VBA code that will allow me to concatenate text from across multiple worksheets. To give some background, I have a survey on an excel worksheet that I send out for team members to complete. I then have a results workbook which contains VBA code to pull all the surveys in and a results worksheet which tallys up all the responses. It all works great for calculating scores, however, I also ask for comments and there are some open questions. My plan is collect up all the written responses with a comma between them.

    I started off with a long formula in each of the cells, see below, which worked for a set number of 20 results with known worksheet names.

    =CONCATENATE('Survey Results 1'!L24,",",'Survey Results 2'!L24,",",'Survey Results 3'!L24,",",'Survey Results 4'!L24,",",'Survey Results 5'!L24,",",'Survey Results 6'!L24,",",'Survey Results 7'!L24,",",'Survey Results 8'!L24,",",'Survey Results 9'!L24,",",'Survey Results 10'!L24,",",'Survey Results 11'!L24,",",'Survey Results 12'!L24,",",'Survey Results 13'!L24,",",'Survey Results 14'!L24,",",'Survey Results 15'!L24,",",'Survey Results 16'!L24,",",'Survey Results 17'!L24,",",#REF!,",",'Survey Results 19'!L24,",",'Survey Results 20'!L24,"")

    I have now created a dynamic Index List on a sheet called Key which updates based on the names of the worksheets. I am thinking some kind of code which will use the Worksheets from the Index List and pull all data from a specific cell, for the above example cell L24 and will concatenate it into a specified cell.

    Is this possible?


    Thanks,
    Adam

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    It's possible: if your summary sheet is named "Summary" -

    Please Login or Register  to view this content.
    But better would be to list each comment in a separate cell, using a formula keyed to your Index List, where the first cell of the list is in cell L2:

    =INDIRECT("'"& L2 & "'!L24")

    Then copy that down to match your index list
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Here is a different approach:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Hi both,

    Thank you for your help although I must admit I am a little lost and not sure how to make the codes above work for my workbook. I have attached my workbook incase either of you (or someone new) is able to amend it to work? I haven't worked on this project for a while but would like to complete it for next week and this is the last step.

    Any help would be much appreciated.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    I have also attached the survey that is sent out in case this makes it easier. You will notice the survey row numbers do not align the 'Results Table' tab on the results workbook, this is due to removing the 'Name (Optional)' row.

    I would like the macro to pull in all comments from applicable cells in column L along with the comments to the questions from row 40 down.

    Many thanks
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Here is a start - I added a button to the sheet "Results Table" to add all the values together from cell L24 on any "Survey Results X" sheet that exists, and place it into cell A39. See if that does what you want.

    Note that your VBA project code had a lot of errors that I fixed so that the macro would run. I may have gotten it correct - I think so - but maybe not
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Hi Bernie, thank you for your help.

    I have tried the macro and cannot get it do what I want. I actually can't see that it does anything, sorry!

    I would like the macro to copy text from cell A40 on any 'Survey Results' sheets that exist and copy it into cell A39. I think once I have the macro working for one cell I will then be able to copy the sub for other cells.

    I have tried to amend the code to:

    Sub FillWhatWentWell()
    Dim wSht As Worksheet
    Dim strResp As String

    For Each wSht In ThisWorkbook.Worksheets
    If wSht.Name Like "*Survey Results*" Then
    strResp = strResp & IIf(strResp <> "", ", ", "") & wSht.Range("A40").Value
    End If
    Next wSht

    ThisWorkbook.Worksheets("Results Table").Range("A39") = strResp
    End Sub

    But I still cannot get it to work.

    Thanks for your help

  8. #8
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Hi Bernie,

    I seem to have corrected it.

    I had to change

    If wSht.Name Like "*Survey Results*" Then

    to

    If wSht.Name Like "*Survey*" Then

    I will keep going and hopefully it will all now work. Thanks

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Do you have an example workbook with at least one 'survey results' sheets inlcuded?

  10. #10
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Hi Bernie,

    Please see attached. I have amended the code and got the text from 'Survey' cells A40 to copy into the 'Results Table' A39. This works great, thank you.

    The next step is to get the macro extended to cover the list below, are you able to show an example of how to do this? I don't expect you to do it all as I expect its a lot of typing.

    The final step, if possible, can this macro run on the 'generate results' button so that it pulls in all the sheets and then pulls all the text from the various 'Survey' worksheets into the 'Results Table'?

    Untitled table.png
    Attached Files Attached Files
    Last edited by help85; 11-27-2020 at 12:40 PM.

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    I have added a table of your list, and modified the macro to loop through the pairs.
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Quote Originally Posted by Bernie Deitrick View Post
    I have added a table of your list, and modified the macro to loop through the pairs.
    This version runs both macros from one button.... I missed that before.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    That works brilliantly, thank you very much!

    Very much appreciated!!

  14. #14
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,945

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    You are very welcome - glad that it works for you, and thanks for letting us know.

  15. #15
    Registered User
    Join Date
    09-30-2020
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    26

    Re: VBA Code for concatenating text from multiple (dynamic no of) worksheets

    Latest version attached.
    Attached Files Attached Files

+ 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. Replies: 2
    Last Post: 03-14-2018, 08:26 AM
  2. Replies: 2
    Last Post: 03-14-2018, 02:19 AM
  3. Dynamic Borders across multiple worksheets
    By sparkyster in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-02-2018, 10:00 PM
  4. [SOLVED] Copy rows from multiple worksheets with multiple criteria - greater than dynamic values
    By stackout in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-03-2015, 04:36 PM
  5. [SOLVED] Goal; Concatenating 5 dynamic columns WITH spaces
    By LouisVBA in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-24-2015, 01:54 PM
  6. VBA For Concatenating Dynamic Rows and Columns
    By imark in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-15-2014, 02:05 PM
  7. Dynamic values in multiple worksheets
    By ninosportif in forum Excel General
    Replies: 0
    Last Post: 04-13-2009, 01:17 AM

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