+ Reply to Thread
Results 1 to 9 of 9

Need Macro to move a row of cells down one row

Hybrid View

  1. #1
    Registered User
    Join Date
    02-25-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Need Macro to move a row of cells down one row

    I need a macro that when I place the cursor in cell A3 will copy cells A3...F3 to A4...F4. And then if I move the cursor to A6 will copy the cells A6…F6 to A7…F7.


    Book1.xls




    Any help would be appreciated.

    Thank you,

    akey

  2. #2
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Need Macro to move a row of cells down one row

    try something like this
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 And Target.Count = 1 Then
    Target.Resize(1, 6).Copy Destination:=Target.Offset(1, 0)
    End If
    End Sub
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  3. #3
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Need Macro to move a row of cells down one row

    try using the bellow macro, it is based on a double click instead of a single click.

    I'm also attaching a workbook to show it working.

    this code will also modify your subtotal formula to include the newly entered row.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim intRow, intLen As Integer
        If Target.Column = 1 Then
            intRow = Target.Row
            Range("A" & intRow + 1).Select
            Selection.EntireRow.Insert
            Range("A" & intRow & ":F" & intRow).Select
            Selection.Copy
            Range("A" & intRow + 1).Select
            ActiveSheet.Paste
            Application.CutCopyMode = False
            intLen = Len(intRow)
            If Right(Range("G" & intRow + 2).Formula, 3 + intLen) = ":G" & intRow & ")" Then
                Range("G" & intRow + 2).Formula = Left(Range("G" & intRow + 2).Formula, Len(Range("G" & intRow + 2).Formula) - 1 - intLen) & intRow + 1 & ")"
            End If
        End If
    End Sub
    Attached Files Attached Files
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  4. #4
    Registered User
    Join Date
    02-25-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Need Macro to move a row of cells down one row

    I tried cutting and pasting your macro but I keep getting a compile error "Expected End Sub"

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    ' Keyboard Shortcut: Ctrl+z
    '
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim intRow, intLen As Integer
    If Target.Column = 1 Then
    intRow = Target.Row
    Range("A" & intRow + 1).Select
    Selection.EntireRow.Insert
    Range("A" & intRow & ":F" & intRow).Select
    Selection.Copy
    Range("A" & intRow + 1).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    intLen = Len(intRow)
    If Right(Range("G" & intRow + 2).Formula, 3 + intLen) = ":G" & intRow & ")" Then
    Range("G" & intRow + 2).Formula = Left(Range("G" & intRow + 2).Formula, Len(Range("G" & intRow + 2).Formula) - 1 - intLen) & intRow + 1 & ")"
    End If
    End If
    End Sub


    Any thoughts?

    Thank you,

    akey

  5. #5
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Need Macro to move a row of cells down one row

    if you want use this code in A3 then A6 then A9 and so on then use this
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 And Target.Count = 1 Then
    If Target.Row - (3 * Int(Target.Row / 3)) = 0 Then
    Target.Resize(1, 6).Copy Destination:=Target.Offset(1, 0)
    End If
    End If
    End Sub

  6. #6
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Need Macro to move a row of cells down one row

    instead of entering the macro into a new module under a sub, you need to enter it into the code view of the sheet (just double click on the sheet name in the VB editor) and paste it there

  7. #7
    Registered User
    Join Date
    02-25-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Need Macro to move a row of cells down one row

    Great thanks it is working. The only problem is I don't want a new line inserted I just want those cells copied down so that they are on the same row as the subtotaled amount. I need the row content in yellow on the attached sheet.

    Book2.xls

    Thanks for all you help,

    akey

  8. #8
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Need Macro to move a row of cells down one row

    in that case you could use this code instead, still based on a double click.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim intRow As Integer
        If Target.Column = 1 Then
            intRow = Target.Row
            Range("A" & intRow & ":F" & intRow).Select
            Selection.Copy
            Range("A" & intRow + 1).Select
            ActiveSheet.Paste
            Application.CutCopyMode = False
        End If
    End Sub

  9. #9
    Registered User
    Join Date
    02-25-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Need Macro to move a row of cells down one row

    Thanks DGagnon, it worked perfectly.

    akey

+ 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