Consulting

Results 1 to 20 of 20

Thread: Solved: script to copy multiple cell values into one cell

  1. #1
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location

    Solved: script to copy multiple cell values into one cell

    Hello
    I'm looking for a script that allows me to paste values from multiple cell to one single cell (when done manually we hit the F2 key when a cell is selected) ........
    example : if i have the following in my MS word
    (please refer attachment)

    by doing just a copy and paste to excel sheet it will paste into 6 different cells
    but if i copy the same and select a cell in excel and hit the F2 key, all of it will paste in one single cell...
    i need a script for the second option @ it should paste the values in one cell without hitting the F2....

    Thanks in advance
    Regards
    Skopweb

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Would it be OK to let it paste into multiple cells, then concatenate it?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    BTW, do you want the copy and paste from Word to Excel to be automated, or will you do that and just let the macrio tidy up?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location

    script to copy multiple cell values into one cell

    Thank you for responding xld.....
    I doubt Concatenate will work for me.

    The copy and paste on selected cell effort will be manual . only thing is that the user should not go for F2 key to paste the values in one cell.
    also Word will be one of the source to copy from as there will be other sources like htm, notepad etc.,

    Regards
    Skopweb

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It seems to me that you want a macro that will concatenate all of the selected cells after the copy paste has been done. Won't that do it?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location
    well i'm not sure. but can you show me what are you reffering to so i can conclude if it suits my needs

    Skopweb

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Something like

    [vba]
    Public Function ConCat()
    Dim tmp As variant
    Dim cell As Range

    For Each cell In Selection

    tmp=tmp & cell.Value
    Next cell

    Selection.ClearContents
    Selection.Cells(1,1).Value = tmp
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location
    hello vbax
    should i enter this code in module,workbook or sheet

    regards
    skopweb

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That was standard code module.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location
    hello xld
    this code is not working. i entered the code in module of excel

    Regards
    Skopweb

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Cange it to a Sub
    Select the cells in Excel you wish to join then run the sub
    [vba]
    Sub ConCat()
    Dim tmp As Variant
    Dim cell As Range

    For Each cell In Selection

    tmp = tmp & cell.Value
    Next cell

    Selection.ClearContents
    Selection.Cells(1, 1).Value = tmp
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location
    Hello Mdmackillop
    your code seems to be working. But i need to run macros macros to perform the concat
    Is there a way it performs automatically as i paste.

    Regards
    Skopweb

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This will work on any worksheet change. I don't know that it can be limited to a Paste event

    [vba]
    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    ConCat Target
    Application.EnableEvents = True
    End Sub

    Sub ConCat(Target)
    Dim tmp As Variant, cell As Range
    For Each cell In Target
    tmp = tmp & cell.Value
    Next cell
    Target.ClearContents
    Target.Cells(1, 1).Value = tmp
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location
    hello mdmackillop
    this seems to be working.
    just one more thing........ is there a way to limit this script to a given column or 2 columns (A or B or A&B) in a sheet

    thanks in advance
    Regards
    Skopweb

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Range)
    if Target.column > 2 then Exit Sub
    Application.EnableEvents = False

    [/VBA]

    With regard to limiting functionality, you could assign Paste and the Concatenate code to a Keyboard Shortcut and use this instead of Control + V.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  16. #16
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location
    thank you sir for you prompt reply but i doubt this will work only for a given column.
    for eg : if i need this code to work on cells in column D only and other columns it should be normal. will this work

    Regards
    Skopweb

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Consider how you might change this line
    [VBA]
    If Target.column > 2 Then Exit Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    VBAX Regular
    Joined
    Jul 2009
    Posts
    58
    Location
    Thank you

    [VBA]If Not (Target.Column = 4) Then Exit Sub[/VBA]

    Regards
    Skopweb

  19. #19
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    ... and if you need different macros for different columns, you could use Select Case on the column numbers and call your macros/Exit as appropriate.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  20. #20
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    1
    Location

    SOLVED

    Quote Originally Posted by Bob Phillips View Post
    Something like

    [vba]
    Public Function ConCat()
    Dim tmp As variant
    Dim cell As Range

    For Each cell In Selection

    tmp=tmp & cell.Value
    Next cell

    Selection.ClearContents
    Selection.Cells(1,1).Value = tmp
    End Function
    [/vba]
    Working solution. excellent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •