+ Reply to Thread
Results 1 to 4 of 4

Insert rows anywhere in existing spreadsheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2006
    Location
    Midlands England
    MS-Off Ver
    97, 2000, 2003
    Posts
    11

    Red face Insert rows anywhere in existing spreadsheet.

    In this spreadsheet there are over 4000 rows. I wish to have a macro that will insert a copy of any row just one row beneath a selected row.

    I have generated copy macros using both and Absolute and Relative Reference methods but neither does what I want it to do. ( See examples below ). Both work the first time but not afterwards

    If I use Absolute references it will only work on the same row as the original ( obviously??).
    If I use Relative references it will work on the selected row plus the off-set of the original selected row.

    I attach an example based on eight lines from the spreadsheet..
    Example 1 is the original.

    Account No Product No Goods Value Tax Total inc Tax Date Shipped Invoice Number Date Invoiced Payment Value Payment Date Notes
    20124 47AB16 120.12 21.02 141.14 11-Jan-07 CL0159 11-Jan-07 141.14 14-Feb-07
    20125 54DE01 23.00 4.03 27.03 20-Jan-07 CL0160 20-Jan-07 27.03 30-Jan-07
    20130 47AB16 120.12 21.02 141.14 20-Jan-07 CL0165 20-Jan-07 100.00 14-Feb-07 41.14 O/S
    20132 47AB16 120.12 21.02 141.14 30-Jan-07 CL0170 30-Jan-07 141.14 22-Feb-07
    20133 63KT21 213.00 37.28 250.28 14-Feb-07 CL0171 14-Feb-07 250.28 10-Mar-07
    20133 47AB16 120.12 21.02 141.14 14-Feb-07 CL0171 14-Feb-07
    20134 14AA23 15.50 2.71 18.21 28-Feb-07 CL0180 28-Feb-07 18.21 20-Mar-07

    Example two is what I wish to achieve. That is:_
    Copy an existing Row ( Row 4 in the example )
    Insert and Paste the Copy of the original row in the next line down.
    ( Copy of Row 4 inserted as new Row 5,
    Original Rows 5 and others pushed down one )
    Insert new Blank row into Row 6. Original Rows 5 and others pushed down one )
    Copy Account Number from original Row 4 and paste in column A of new Row 6
    Write "Con Sol" or some other text into Column B of new Row
    End of Macro.
    Then add further info on the added line.
    Here is what I am trying to achieve as Example 2.
    Account No Product No Goods Value Tax Total inc Tax Date Shipped Invoice Number Date Invoiced Payment Value Payment Date Notes
    20124 47AB16 120.12 21.02 141.14 11-Jan-07 CL0159 11-Jan-07 141.14 14-Feb-07
    20125 54DE01 23.00 4.03 27.03 20-Jan-07 CL0160 20-Jan-07 27.03 30-Jan-07
    20130 47AB16 120.12 21.02 141.14 20-Jan-07 CL0165 20-Jan-07 100.00 14-Feb-07 41.14 O/S
    20130 47AB16 120.12 21.02 141.14 20-Jan-07 CL0165 20-Jan-07 100.00 14-Feb-07 41.14 O/S
    20130 Con Sol Add further info
    20132 47AB16 120.12 21.02 141.14 30-Jan-07 CL0170 30-Jan-07 141.14 22-Feb-07
    20133 63KT21 213.00 37.28 250.28 14-Feb-07 CL0171 14-Feb-07 250.28 10-Mar-07
    20133 47AB16 120.12 21.02 141.14 14-Feb-07 CL0171 14-Feb-07
    20134 14AA23 15.50 2.71 18.21 28-Feb-07 CL0180 28-Feb-07 18.21 20-Mar-07

    I have been trying to "automate" this for months.

    Can any one advise on how to achieve this please?
    Here are copies of the two attempted macros.
    Example 1
    Sub Macro3()
    '
    ' Macro3 Macro
    ' Macro recorded 21/08/2007 by Wej 3
    '
    ' Keyboard Shortcut: Ctrl+p
    ' Using Relative addressing ????
    '
    '
        ActiveCell.Offset(-4, 0).Rows("1:1").EntireRow.Select
        Selection.Copy
        ActiveCell.Rows("1:1").EntireRow.Select
        Selection.Insert Shift:=xlDown
        ActiveCell.Offset(2, 0).Rows("1:1").EntireRow.Select
        Application.CutCopyMode = False
        Selection.Insert Shift:=xlDown
        ActiveCell.Offset(-1, 0).Range("A1").Select
        Selection.Copy
        ActiveCell.Offset(1, 0).Range("A1").Select
        ActiveSheet.Paste
        ActiveCell.Offset(0, 10).Range("A1").Select
        Application.CutCopyMode = False
        ActiveCell.FormulaR1C1 = "Relative"
        ActiveCell.Offset(5, -10).Range("A1").Select
    End Sub
    
    Example 2
    Sub Macro4()
    '
    ' Macro4 Macro
    ' Macro recorded 21/08/2007 by Reservations 3
    '
    ' Keyboard Shortcut: Ctrl+o
    '
    ' Using Absolute addressing ????
    '
        Rows("3:3").RowHeight = 13.5
        Rows("4:4").Select
        Selection.Copy
        Rows("5:5").Select
        Selection.Insert Shift:=xlDown
        Rows("6:6").Select
        Application.CutCopyMode = False
        Selection.Insert Shift:=xlDown
        Range("A5").Select
        Selection.Copy
        Range("A6").Select
        ActiveSheet.Paste
        Range("K6").Select
        Application.CutCopyMode = False
        ActiveCell.FormulaR1C1 = "Non-Relative"
        Range("A11").Select
    End Sub
    
    
    
    End of message

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Try this

    Sub Test()
    Selection.Copy
    Rows(Selection.Row + 1).Insert Shift:=xlDown
    Application.CutCopyMode = False
    Rows(Selection.Row + 2).Insert Shift:=xlDown
    Cells(Selection.Row + 2, 1) = Cells(Selection.Row, 1)
    Cells(Selection.Row + 2, 2) = "Con Sol"
    End Sub
    Martin

  3. #3
    Registered User
    Join Date
    01-27-2006
    Location
    Midlands England
    MS-Off Ver
    97, 2000, 2003
    Posts
    11

    Smile insert copy of a row anywhere in a spreadsheet

    Looks good!
    Works in test conditions. I will test it further tomorrow and then try to understand it!

    I have one quick question. Was this generated using the "Record Macro" facility or created by you from scratch?

    Thank you very much.
    wejofost@aol.com

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Actually a bit of both. A quick record and then an edit. It's often the quickest way of putting these sort of things together. For more complex operations, such as charting, its often much more simple to record and then edit the parameters.

+ 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