Results 1 to 13 of 13

Insert rows based on the current cell value and, then, copy some cells into the new rows

Threaded View

  1. #1
    Registered User
    Join Date
    11-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    8

    Insert rows based on the current cell value and, then, copy some cells into the new rows

    Hello All,

    I am a fairly experienced Excel user, but new to VBA and macros. I have recorded some macros and done a bit of editing of the VBA code.

    I am trying to create a macro that will, based on the value of the current cell, insert X minus 1 new rows below the current row, where X is the value of the current cell and, then, copy some cells from the current row straight down into the new blank rows.

    For example, if the data is located in the range J10:O13 as follows and J10 is the starting cell:

    ___J__K__L__M__N__O

    10__3__X__X__X__X__X
    11__2__X__X__X__X__X
    12__3__X__X__X__X__X
    13__1__X__X__X__X__X


    The macro would change the data to:

    ___J__K__L__M__N__O

    10__3__X__X__X__X__X
    11_____X__X__X__X__X
    12_____X__X__X__X__X
    13__2__X__X__X__X__X
    14_____X__X__X__X__X
    15__3__X__X__X__X__X
    16_____X__X__X__X__X
    17_____X__X__X__X__X
    18__1__X__X__X__X__X


    Note: I had to include the underscores above just to get the columns to line up. Only the cells in columns K thru O are copied down into the new rows, not the cells in column J.


    I did find the following macro which works perfectly to insert the new rows, but I cannot seem to add the proper code to get the specified cells from the current row to copy down into the new cells:



    Sub Insert_Rows()
    '
    ' Insert_Rows Macro
    '
    ' Keyboard Shortcut: Ctrl+i
    '
    Dim i As Integer, n As Integer, m As Long, currentCell As Range
        Set currentCell = ActiveCell
        Do While Not IsEmpty(currentCell)
            n = currentCell.Value - 1
            m = currentCell.Row
            If n > 0 Then
                Rows(m + 1 & ":" & m + n).Insert
                Set currentCell = currentCell.Offset(n + 1, 0)
            Else
                Set currentCell = currentCell.Offset(1, 0)
            End If
        Loop
    
    End Sub

    I have read that it is better to start at the bottom of the data and move up when inserting rows and I do wonder if that may be related to my problem of not getting the copy/paste to work properly.

    Here is the code that I tried to add for copying and pasting:


    ActiveCell.Offset(0, 1).Range("A1:K1").Select
                Selection.Copy
                ActiveCell.Offset(1, 0).Range("A1:K3").Select
                ActiveSheet.Paste
    It does copy and paste, but the pasted data keeps shifting one column to the right each time the macro loops.

    Thanks for any help anyone can provide!

    RZ
    Last edited by Excel-RZ; 11-03-2013 at 12:48 PM. Reason: code tags added

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] insert rows based on number in cell and copy the data down into the new rows
    By pziefle in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-05-2013, 11:19 AM
  2. Insert rows based on a value in a cell and copy the data down into the new rows
    By eyeman96 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2013, 07:54 AM
  3. Excel Macro to insert two rows based on condition and copy and paste multiple cells
    By mannabhalo in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-13-2012, 12:56 PM
  4. Insert a number of rows based on a cell value and copy formulas on multiple worksheets
    By Charleneq40 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2012, 12:45 PM
  5. [SOLVED] Insert Multiple Rows Based Off Number in Cell and Copy Data From Above New Rows
    By tstell1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2012, 04:15 PM

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