+ Reply to Thread
Results 1 to 6 of 6

Insert two rows above cell, if cell in column A contains data

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2013
    Location
    Normal, IL
    MS-Off Ver
    Excel 2010
    Posts
    3

    Insert two rows above cell, if cell in column A contains data

    I have a worksheet that is not exporting nicely. I would like to create a macro that inserts two rows above each cell containing data in column A so that I can add totals to lines D & E. Please see attached for better illustration.

    Any suggestions much appreciated!

    Thanksexample.xlsxexample.xlsx

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Insert two rows above cell, if cell in column A contains data

    Try this code
    Option Explicit
    
    Sub insert_rows()
    Dim i As Long, lrow As Long
    
    Application.ScreenUpdating = False
    
    With Worksheets("Sheet1")
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = lrow To 2 Step -1
            If .Range("A" & i).Value <> "" Then .Rows(i & ":" & i - 1).Insert
        Next i
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    07-10-2013
    Location
    Normal, IL
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Insert two rows above cell, if cell in column A contains data

    Can you write it to start on line A10? The code is adding two rows, but not corresponding with the cell containing data in column A. It appears to add the 2 lines above the last cell of data in line B.

    Thank you for your help! I really appreciate it!
    Last edited by ttroemel1; 07-10-2013 at 02:35 PM.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Insert two rows above cell, if cell in column A contains data

    Change
    For i = lrow To 2 Step -1
    to
    For i = lrow To 10 Step -1

  5. #5
    Registered User
    Join Date
    07-10-2013
    Location
    Normal, IL
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Insert two rows above cell, if cell in column A contains data

    Please see example for where the code is adding the lines. The two lines should be added above row 8, not row 7. Any ideas on how to fix?

    Thanks!example 1.xlsx

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Insert two rows above cell, if cell in column A contains data

    It is adding the rows above row 8 and not row 7.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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