+ Reply to Thread
Results 1 to 6 of 6

Thread: Inserting row based on condition

  1. #1
    Registered User
    Join Date
    06-24-2008
    Posts
    19

    Inserting row based on condition

    Hey All,

    I've sifting through some older posts on here and I came across some code that has helped me get a basis for what I'm trying to accomplish.

    Below is the working code but I want to insert a line wherever I encounter a number. I have a range of cells in column "A" with invoices listed and at the end of each customer I have the total. At the total line (the number) I want to insert a line. Right now I can't figure out how get the cell value to recognize a number from a non-number data type (bolded area)

    TIA!

    Sub InsertByCriteria()
    Dim lastrow As Long
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
        
    For i = lastrow To 2 Step -1
        If Cells(i, 1).Value > 0# _
            Then Rows(i).EntireRow.Insert Shift:=xlDown
    Next i
     
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    End Sub
    Last edited by XL2008; 05-04-2009 at 01:03 PM.

  2. #2
    Forum Guru
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2003, 2007.
    Posts
    1,462

    Re: Inserting row based on condition

    XL2008,

    Please post your workbook, or a sample of it - scroll down and see "Manage Attachments".
    Have a great day,
    Stan
    stanleydgromjr
    Windows Vista Business, Excel 2003 and 2007

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Inserting row based on condition

    XL2008, be sure to go back and EDIT that first post, click GO ADVANCED (if necessary) and highlight all that code...then click the # icon to wrap the code in [/code] tags. It's a forum requirement and it makes the code readable in native format.

    ========

    Then try this:
    Sub InsertByCriteria()
    Dim LastRow As Long, i As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
        For i = LastRow To 2 Step -1
            If IsNumeric(Cells(i, 1).Value) Then Rows(i).EntireRow.Insert Shift:=xlDown
        Next i
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    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!)

  4. #4
    Registered User
    Join Date
    06-24-2008
    Posts
    19

    Re: Inserting row based on condition

    That works perfect except I realized I didn't account for one thing. I need the inserted row to be the row after the row that has the number condition. Instead it is inserting the blank row before that number condition.

    Thank you again for all your help!

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Inserting row based on condition

    How about this little tweak:
    Sub InsertByCriteria()
    Dim LastRow As Long, i As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
        For i = LastRow To 2 Step -1
            If IsNumeric(Cells(i, 1).Value) Then Rows(i + 1).EntireRow.Insert Shift:=xlDown
        Next i
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    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!)

  6. #6
    Registered User
    Join Date
    06-24-2008
    Posts
    19

    Re: Inserting row based on condition

    Thank you again for all your help!

+ 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.2.0