+ Reply to Thread
Results 1 to 3 of 3

Insert rows between existing data

Hybrid View

  1. #1
    Registered User
    Join Date
    09-20-2010
    Location
    Auckland
    MS-Off Ver
    Excel 2003
    Posts
    1

    Insert rows between existing data

    Hi there,

    I am wanting to insert a whole new row between each row in the existing data (say A2:A10) - A10 being the last cell used in the column

    On this new row I am wanting to insert some formulas such as in cell A3 =IF(A2>5,Yes,No).

    I am then wanting this to loop through for all the cells in existing data range.

    I have looked through the existing posts on this and am still a litttle stuck. I have been able to insert the spacing rows but not not insert the formula beneath.

    Im not new to excel, but new to VBA. Any help would be kindly appreciated.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Insert rows between existing data

    Try this:
    Option Explicit
    
    Sub InsertRowsAndFormula()
    Dim LR As Long  'last row with data
    Dim RW As Long
    
    'Add blank rows
        LR = Range("A" & Rows.Count).End(xlUp).Row + 1
        For RW = LR To 2 Step -1
            Rows(RW).Insert xlShiftDown
        Next RW
    
    'Add formulas
        LR = Range("A" & Rows.Count).End(xlUp).Row + 1
        Range("A2:A" & LR).SpecialCells(xlBlanks) _
            .FormulaR1C1 = "=IF(R[-1]C>5, ""yes"", ""no"")"
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    09-01-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: Insert rows between existing data

    
    Private Sub Insert()
    
    Dim row, count, column As Integer
    Dim formula As String
    ' not sure which column you want, a=1, b=2 and so on.
    
    column = 1 ' you specify
    
    count = 1 ' or whichever row you want to start in
    
    row = ActiveSheet.UsedRange.Rows.count
    row = row * 2
    
    formula = "=IF(A2>5,Yes,No)"
    
    Do Until count = row
    count = count + 1
    Cells(count, column).EntireRow.Insert
    
    
    If count = row Then
    
    Exit Sub
    
    End If
    
    Cells(count, column).Select
    Cells(count, column).formula = formula
    count = count + 1
    
    
    
    Loop
    
    end sub
    ?

    Bit of trouble with the loop... Should get you going in the right direction.
    Last edited by tenk283; 09-20-2010 at 03:57 AM. Reason: Nicked some of JBeaucaire's code ;)

+ 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