Consulting

Results 1 to 4 of 4

Thread: Automating Copy And Paste

  1. #1

    Automating Copy And Paste

    Hey Guys,

    I was just wondering if anyone knows how to run the below for every row that has a value init on Sheet 1?

    so it would be

    E2,C2,L2,P2,Q2
    E3,C3,L3,P3,Q3
    E4,C4,L4,P4,Q4
    E5,C5,L5,P5,Q5
    E6,C6,L6,P6,Q6
    E7,C7,L7,P7,Q7 and so on it there is no values left.

    These cells would always be copied into the same range on Sheet 2

    E2- B7
    C2-B21
    L2-C21
    P2-D21
    Q2-E21

    I have attached a very rudimentry macro for it but didnt know if there is a quicker way of doing it.
    Sheets("Sheet1").Select    Range("E2").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("B7").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("C2").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("B21").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("L2").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("C21").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("P2").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("D21").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("Q2").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("E21").Select
        ActiveSheet.Paste
        Range("E26").Select
        Sheets("Sheet2").Select
        Range("B7").Select
        Selection.ClearContents
            Sheets("Sheet1").Select
        Range("E3").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("B7").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("C3").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("B21").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("L3").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("C21").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("P3").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("D21").Select
        ActiveSheet.Paste
        Sheets("Sheet1").Select
        Range("Q3").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet2").Select
        Range("E21").Select
        ActiveSheet.Paste

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    You can clean up the macro recorder code a lot

    Usually you do not need to .Select something to use it

    Option Explicit
    Sub Test()
        With Sheets("Sheet1")
            .Range("E2").Copy Sheets("Sheet2").Range("B7")
            .Range("C2").Copy Sheets("Sheet2").Range("B21")
            
    '       repeat above
            
            
        End With
        Application.CutCopyMode = False
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
    Dim arrA()
    Dim arrB()
    Dim i As Long
    
    
    arrA = Array("E2", "C2", "L2")
    arrB = Array("B7", "B21", "C21")
    
    
    For i = 0 To 2
    Sheet1.Range(arrA(i)).Copy Sheet2.Range(arrB(i))
    Next i
    End Sub
    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'

  4. #4
    Thank you Guys Really Appreciate it.

Tags for this Thread

Posting Permissions

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