SOLVED

macro to copy and paste on loop example sent

%3CLINGO-SUB%20id%3D%22lingo-sub-1620234%22%20slang%3D%22en-US%22%3Emacro%20to%20copy%20and%20paste%20on%20loop%20example%20sent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1620234%22%20slang%3D%22en-US%22%3E%3CP%3Eread%20note%20in%20example%20file.%20sheet%201%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1620234%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1620384%22%20slang%3D%22en-US%22%3ERe%3A%20macro%20to%20copy%20and%20paste%20on%20loop%20example%20sent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1620384%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F144248%22%20target%3D%22_blank%22%3E%40Alan%20Skelly%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHello%2C%20I%20hope%20I%20can%20help%20you%3C%2FP%3E%3CP%3EWell%2C%20to%20be%20honest%20I%20didn't%20understand%20the%20main%20purpose%20of%20transfer%20all%20values%20from%201%20cell%20to%20to%20another%20via%20code.%3C%2FP%3E%3CP%3EBut%20I%20can%20presume%20you%20are%20looking%20for%20something%20that%20can%20take%20piece%20of%20piece%20and%20then%20do%20this%20transferring.%20I%20prepare%20the%20following%20loop%20so%20you%20can%20use%20in%20your%20workbook.%3C%2FP%3E%3CP%3ERemember%20that%20what%20this%20code%20is%20doing%20is%20breaking%20the%20names%20separated%20by%20comma%20and%20transferring%20one%20by%20one%20to%20another%20cell%2C%20just%20like%20you%20wrote.%20Please%20advise%20me%20if%20is%20something%20different%20from%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20TranferValuesbyLoop()%0A%20%20%20%20Dim%20ListOfNames()%20As%20String%0A%20%20%20%20ListOfNames%20%3D%20Split(Worksheets(%22sheet1%22).Range(%22D4%22).Value%2C%20%22%2C%20%22)%0A%20%20%20%20%0A%20%20%20%20Dim%20item%20As%20Variant%2C%20Destination%20As%20Range%0A%20%20%20%20Set%20Destination%20%3D%20Worksheets(%22sheet2%22).Range(%22E4%22)%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20For%20Each%20item%20In%20ListOfNames%0A%20%20%20%20%20%20%20%20If%20Len(Destination)%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Destination.Value%20%3D%20Destination.Value%20%26amp%3B%20%22%2C%22%20%26amp%3B%20item%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20Destination.Value%20%3D%20item%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20item%0A%20%20%20%20%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1620402%22%20slang%3D%22en-US%22%3ERe%3A%20macro%20to%20copy%20and%20paste%20on%20loop%20example%20sent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1620402%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%20for%20response%20works%20good%2C%20but%20my%20fault%20in%20explanation.%20I%20don't%20want%20to%20have%20sheet%202%20look%20like%20sheet%201%20in%20end.%20I%20want%20first%20name%20pasted%20then%20next%20pasted%20over%20that.%20Jim%20Smith%20pasted%20in%20then%20Ann%20waash%20over%20that.%20not%20jim%20smith%2C%20then%20ann%20waash%20added.%26nbsp%3B%20jim%20smith%20pasted%20in%20cell%20I'll%20run%20macro%20associated%20with%20jim%20smith.%20then%20loop%20around%20paste%20in%20ann%20waash%20over%20jim%20smith%20and%20I'll%20run%20macro%20associated%20with%20ann%20waash.%20until%20last%20name%20is%20done.%20Also%20the%20paste%20has%20to%20be%20paste%20special.values.%20Thanks%20again%20for%20looking%20at%20this%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F767933%22%20target%3D%22_blank%22%3E%40Juliano-Petrukio%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1620468%22%20slang%3D%22en-US%22%3ERe%3A%20macro%20to%20copy%20and%20paste%20on%20loop%20example%20sent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1620468%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F144248%22%20target%3D%22_blank%22%3E%40Alan%20Skelly%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELooks%20like%20you%20want%20to%20invert%20the%20order%20of%20the%20names%26nbsp%3B%3C%2FP%3E%3CP%3EOriginal%3A%20Jim%20Smith%2C%20Annn%20wash%2C%20Hammerton%2C%20Polhollow%2C%20Undermam%3C%2FP%3E%3CP%3EResult%3A%20Undermam%2C%20Polhollow%2C%20Hammerton%2C%20Annn%20wash%2C%20Jim%20Smith%3C%2FP%3E%3CP%3ERegarding%20Paste%20Values%20only%20you%20don't%20need%20to%20worry%20because%20the%20values%20are%20stored%20in%20an%20variable%20called%26nbsp%3BListOfNames.%20Only%20the%20values%20are%20stored%20there.%20As%20looks%20like%20you%20want%20to%20invert%20the%20order%2C%20the%20function%26nbsp%3BReverseArray%20does%20that%20and%20then%20each%20value%20of%20the%20reversed%20list%20is%20%22paste%22%20in%20the%20cell%20E4%20of%20the%20sheet2.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EOption%20Explicit%0A%0ASub%20TranferValuesbyLoop()%0A%20%20%20%20Dim%20ListOfNames%20As%20Variant%0A%20%20%20%20ListOfNames%20%3D%20Split(Worksheets(%22sheet1%22).Range(%22D4%22).Value%2C%20%22%2C%20%22)%0A%20%20%20%20ListOfNames%20%3D%20ReverseArray(ListOfNames)%0A%20%20%20%20Dim%20item%20As%20Variant%2C%20Destination%20As%20Range%0A%20%20%20%20Set%20Destination%20%3D%20Worksheets(%22sheet2%22).Range(%22E4%22)%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20For%20Each%20item%20In%20ListOfNames%0A%20%20%20%20%20%20%20%20If%20Len(Destination)%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Destination.Value%20%3D%20Destination.Value%20%26amp%3B%20%22%2C%20%22%20%26amp%3B%20item%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20Destination.Value%20%3D%20item%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20item%0A%20%20%20%20%0AEnd%20Sub%0A%0AFunction%20ReverseArray(arr%20As%20Variant)%20As%20Variant%0A%20%20%20%20Dim%20val%20As%20Variant%0A%0A%20%20%20%20With%20CreateObject(%22System.Collections.ArrayList%22)%0A%20%20%20%20%20%20%20%20For%20Each%20val%20In%20arr%0A%20%20%20%20%20%20%20%20%20%20%20%20.Add%20val%0A%20%20%20%20%20%20%20%20Next%20val%0A%20%20%20%20%20%20%20%20.Reverse%0A%20%20%20%20%20%20%20%20ReverseArray%20%3D%20.Toarray%0A%20%20%20%20End%20With%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1620493%22%20slang%3D%22en-US%22%3ERe%3A%20macro%20to%20copy%20and%20paste%20on%20loop%20example%20sent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1620493%22%20slang%3D%22en-US%22%3E%3CP%3EI%20don't%20know%20where%20you%20got%20that%20from%20my%20explanation.%26nbsp%3B%20Trying%20to%20think%20how%20I%20can%20explain.%26nbsp%3B%20Put%20Jim%20smith%26nbsp%3B%20in%20sheet%202%20.%20that%20will%20trigger%20a%20macro%20I%20have%20for%20jim%20smith%20to%20run.%20jim%20smith%20macro%20runs.%20then%20put%20ann%20waaash%20into%20sheet2%20(jim%20smith%20goes%20away).%20triggering%20a%20macro%20for%20ann%20waash%20to%20run.%20Then%20put%20hammerton%20in%20sheet2%20(ann%20waash%20goes%20away).%20triggering%20a%20macro%20for%20hammerton%20to%20run.%20same%20until%20last%20name%20in%20string%20is%20done.%26nbsp%3B%20I%20am%20putting%20code%20to%20run%20when%20the%20cell%20value%20of%20sheet%202%20changes%26nbsp%3B%20I%20do%20not%20want%20a%20range%20of%20names%20in%20sheet%202%20like%20in%20sheet%201.%20I%20want%20first%20name%20brought%20over%20then%20the%20second%20name%20erasing%20the%20first%20name%20so%20on%20and%20so%20on%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F767933%22%20target%3D%22_blank%22%3E%40Juliano-Petrukio%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1620511%22%20slang%3D%22en-US%22%3ERe%3A%20macro%20to%20copy%20and%20paste%20on%20loop%20example%20sent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1620511%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20I%20was%20able%20to%20make%20changes%20to%20your%20first%20code%20and%20made%20it%20work%20ty%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F144248%22%20target%3D%22_blank%22%3E%40Alan%20Skelly%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

read note in example file. sheet 1

5 Replies
Best Response confirmed by Alan Skelly (Contributor)
Solution

@Alan Skelly 

Hello, I hope I can help you

Well, to be honest I didn't understand the main purpose of transfer all values from 1 cell to to another via code.

But I can presume you are looking for something that can take piece of piece and then do this transferring. I prepare the following loop so you can use in your workbook.

Remember that what this code is doing is breaking the names separated by comma and transferring one by one to another cell, just like you wrote. Please advise me if is something different from this.

 

Sub TranferValuesbyLoop()
    Dim ListOfNames() As String
    ListOfNames = Split(Worksheets("sheet1").Range("D4").Value, ", ")
    
    Dim item As Variant, Destination As Range
    Set Destination = Worksheets("sheet2").Range("E4")
        
    For Each item In ListOfNames
        If Len(Destination) > 0 Then
            Destination.Value = Destination.Value & "," & item
        Else
            Destination.Value = item
        End If
    Next item
    
End Sub

 

Thank you for response works good, but my fault in explanation. I don't want to have sheet 2 look like sheet 1 in end. I want first name pasted then next pasted over that. Jim Smith pasted in then Ann waash over that. not jim smith, then ann waash added.  jim smith pasted in cell I'll run macro associated with jim smith. then loop around paste in ann waash over jim smith and I'll run macro associated with ann waash. until last name is done. Also the paste has to be paste special.values. Thanks again for looking at this@Juliano-Petrukio 

@Alan Skelly 

Looks like you want to invert the order of the names 

Original: Jim Smith, Annn wash, Hammerton, Polhollow, Undermam

Result: Undermam, Polhollow, Hammerton, Annn wash, Jim Smith

Regarding Paste Values only you don't need to worry because the values are stored in an variable called ListOfNames. Only the values are stored there. As looks like you want to invert the order, the function ReverseArray does that and then each value of the reversed list is "paste" in the cell E4 of the sheet2.

 

Option Explicit

Sub TranferValuesbyLoop()
    Dim ListOfNames As Variant
    ListOfNames = Split(Worksheets("sheet1").Range("D4").Value, ", ")
    ListOfNames = ReverseArray(ListOfNames)
    Dim item As Variant, Destination As Range
    Set Destination = Worksheets("sheet2").Range("E4")
        
    For Each item In ListOfNames
        If Len(Destination) > 0 Then
            Destination.Value = Destination.Value & ", " & item
        Else
            Destination.Value = item
        End If
    Next item
    
End Sub

Function ReverseArray(arr As Variant) As Variant
    Dim val As Variant

    With CreateObject("System.Collections.ArrayList")
        For Each val In arr
            .Add val
        Next val
        .Reverse
        ReverseArray = .Toarray
    End With
End Function

 

I don't know where you got that from my explanation.  Trying to think how I can explain.  Put Jim smith  in sheet 2 . that will trigger a macro I have for jim smith to run. jim smith macro runs. then put ann waaash into sheet2 (jim smith goes away). triggering a macro for ann waash to run. Then put hammerton in sheet2 (ann waash goes away). triggering a macro for hammerton to run. same until last name in string is done.  I am putting code to run when the cell value of sheet 2 changes  I do not want a range of names in sheet 2 like in sheet 1. I want first name brought over then the second name erasing the first name so on and so on@Juliano-Petrukio 

Thanks I was able to make changes to your first code and made it work ty@Alan Skelly