+ Reply to Thread
Results 1 to 6 of 6

Macro Loop and Paste in next empty row

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2008
    Posts
    3
    Right..

    Ive been having a play - close but no cigar.

    the code I have at the moment is

    Sub reformat_data()
    '
    ' reformat_data Macro
    '
    ' Keyboard Shortcut: Ctrl+p
    '
    odc = 1
    maxapps = 2
    While Range("A" & odc) <> ""
        odc = odc + 1
        MsgBox "odc value is: " & odc
        Range("A" & odc & ":G" & odc).Select
        Selection.Copy
        Sheets("Sheet2").Select
        Range("C2").Select
        ActiveSheet.Paste
        
        Range("C5:J37").Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Sheet4").Select
        Range("A" & maxapps).Select
        Selection.PasteSpecial Paste:=xlPasteValues
        
        maxapps = maxapps + 34
        
    Wend
    
        
    End Sub
    BUT... it does not select the next row and copy/paste the range into sheet2!c5 correctly :0(
    After the 1st attempt, odc appears to be incrementing (as the message box shows this), but it does not select/copy/paste the correct range - it appears as it is trying to copy the same range :0s

    Any ideas?

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Rayhoop,

    Not sure I follow the logic of the code. Can you upload a sample workbook with a before and after or see if the below helps

    Usually you can find the last cell with this statement

    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    'Where A = Last Value in Col A
    This link explains in more detail

    http://www.rondebruin.nl/copy1.htm

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    05-14-2008
    Posts
    3
    Cheers mate.

    I replaced the 'find last' with a crude method of selecting a cell 34 rows down using
    maxapps = maxapps +34
    34 because the number of rows selected from sheet2 were 33. However in the cut down demo spreadsheet I've only included a handful.

    My end loop statment doesnt work either :0( I have to 'escape' out of the macro.

    odc stands for 'original data cell', so in theory incrementing it by 1 would push the cell range down a row?

    Thanks again for your help.
    Attached Files Attached Files

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Still not clear what your after. This copies Sheet 1 to Sheet 4. Note's may help

    Sub CopyToNewSheet()
    Dim SrcLastRow As Long
    Dim DestLastRow As Long
    Dim SrcSht As Worksheet
    Dim DestSht As Worksheet
    
    'Source data worksheet
    Set SrcSht = Sheets("Sheet1")
    'Destination worksheet
    Set DestSht = Sheets("Sheet4")
    
    'Source data Last Row using Column A
    SrcLastRow = SrcSht.Cells(Rows.Count, "A").End(xlUp).Row
    
    'Destination sheet Last Row using Column A
    DestLastRow = DestSht.Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    'Copy Source sheet A2 to G and Last row to next row in Destination sheet
    SrcSht.Range("A2:G" & SrcLastRow).Copy _
    Destination:=DestSht.Range("A" & DestLastRow)
    
    End Sub
    VBA Noob

+ 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