+ Reply to Thread
Results 1 to 12 of 12

Loop copy and paste

Hybrid View

ScabbyDog Loop copy and paste 03-03-2013, 01:34 PM
Norie Re: Loop copy and paste 03-03-2013, 01:56 PM
ScabbyDog Re: Loop copy and paste 03-03-2013, 01:59 PM
Mumps1 Re: Loop copy and paste 03-03-2013, 02:07 PM
ScabbyDog Re: Loop copy and paste 03-03-2013, 02:19 PM
ScabbyDog Re: Loop copy and paste 03-03-2013, 02:32 PM
AB33 Re: Loop copy and paste 03-03-2013, 02:45 PM
ScabbyDog Re: Loop copy and paste 03-03-2013, 03:05 PM
ScabbyDog Re: Loop copy and paste 03-03-2013, 02:52 PM
ScabbyDog Re: Loop copy and paste 03-03-2013, 02:56 PM
AB33 Re: Loop copy and paste 03-03-2013, 03:00 PM
AB33 Re: Loop copy and paste 03-03-2013, 03:11 PM
  1. #1
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Loop copy and paste

    I need a macro that copies range O1:O21 on sheet1 and then goes to sheet2 and checks to see if B2 is blank. If it is blank paste values into b2. If it is not then shift cells right and then paste values into b2. After it pastes the values, repeat the above 500 times. Possible?
    Last edited by ScabbyDog; 03-03-2013 at 01:38 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Loop copy and paste

    Are you always pasting into B2?
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Loop copy and paste

    Yes norie. Always b2, just the column will be shifted to the right so that b2 can be used for the new data.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,050

    Re: Loop copy and paste

    Hi Scabby Dog. I have de-activated the lines that will make the macro loop 500 times because I'm not sure if this is what you wanted. Have a look at the code and let me know if it suits your purpose.
    Sub Test()
        'Dim x As Integer
        'For x = 1 To 500
            If Sheets("Sheet2").Range("B2") = "" Then
                Sheets("Sheet1").Range("O1:O21").Copy Sheets("Sheet2").Range("B2")
            ElseIf Sheets("Sheet2").Range("B2") <> "" Then
                Sheets("Sheet2").Range("B2").Select
                Selection.EntireColumn.Insert
                Sheets("Sheet1").Range("O1:O21").Copy Sheets("Sheet2").Range("B2")
            End If
        'Next x
    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Loop copy and paste

    I need it to paste special, values as sheet1 contains formulas which it is currently pasting with your code.

  6. #6
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Loop copy and paste

    Another option would be, instead of shifting the column right, just paste the values into the next column that is empty in the range b2:zz22

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop copy and paste

    Try this one

    Sub Test()
        Dim ms As Worksheet
        Application.ScreenUpdating = 0
        Set ms = Sheets("Sheet1")
       
        With Sheets("Sheet2")
            If Sheets("Sheet2").Range("B2") = vbNullString Then
                ms.Range("O1:O21").Resize(500).Copy
                .Range("B2").Resize(500).PasteSpecial xlValues
            Else
                .Range("B2").EntireColumn.Insert
                 ms.Range("O1:O21").Resize(500).Copy
                .Range("B2").Resize(500).PasteSpecial xlValues
            End If
           Application.CutCopyMode = 0
        End With
        Set ms = Nothing
        Application.ScreenUpdating = 1
    End Sub

  8. #8
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Loop copy and paste

    Thanks Ab! We are close now. How would I add the loop to make it run 500 times now?

  9. #9
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Loop copy and paste

    Doesnt work. It inserts an entire column rather than shifting the cells to the right and it only runs once and not 500 times.

  10. #10
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Loop copy and paste

    I have this which works but doesn't paste values.

    Sub Test()
        Dim rng As Range
        
        With ThisWorkbook.Worksheets("Sheet2")
        If Range("B2") = "" Then
            Set rng = Range("B2")
        Else
            Set rng = ThisWorkbook.Worksheets("Sheet2").Range("B2").End(xlToRight).Offset(0, 1)
        End If
        End With
        
        With ThisWorkbook.Worksheets("Sheet1")
        .Range("P3:P22").Copy rng
       
        End With
        
        Set rng = Nothing
        
    End Sub

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop copy and paste

    .Range("P3:P22").Copy
        rng.PasteSpecial xlValues

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop copy and paste

    I am not sure which bit you want to repeat, but you can loop 500 times-500 rows

    Sub Test()
        Dim rng As Range, i As Long
        
        For i = 1 To 500
        With ThisWorkbook.Worksheets("Sheet2")
        If Range("B2") = "" Then
            Set rng = Range("B2")
        Else
            Set rng = ThisWorkbook.Worksheets("Sheet2").Range("B2").End(xlToRight).Offset(0, 1)
        End If
        End With
        
        With ThisWorkbook.Worksheets("Sheet1")
        .Range("P3:P22").Copy
        rng.PasteSpecial xlValues
       
        End With
        Next i
        Set rng = Nothing
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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