+ Reply to Thread
Results 1 to 6 of 6

Need VBA Code for Adding / Deleting Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    03-09-2012
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    41

    Need VBA Code for Adding / Deleting Rows

    Ok - I have 2 parts that I need for Adding / Deleting Rows to a formula based worksheet. (If you can find a simplier option, I would appreciate)

    Part 1 - VBA CODE - Based on a value in a cell ("ADD")(Column R) - copy the row above and insert copied cells (shift cells down) - Need to loop until "NO ADD") (need help with formula for the cell to get "ADD" - I have ****=IF(AND(ISTEXT(A9),ISNUMBER(A8)),"ADD","NO ADD") **** However when new row is copied down I get ****=IF(AND(ISTEXT(A10),ISNUMBER(A8)),"ADD","NO ADD") **** Again the copying needs to loop but ISNumber(stays the same) needs to be one less than ISTEXT()

    Part 2 - This is the code I have to delete the rows and it works

    Sub DELETEROWS()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
    
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        With ActiveSheet
    
    
            Firstrow = 5
            Lastrow = 500
    
            For Lrow = Lastrow To Firstrow Step -1
    
                With .Cells(Lrow, "Q")
    
                    If Not IsError(.Value) Then
    
                        If .Value = "DELETE" Then .EntireRow.Delete
                        'This will delete each row with the Value "delete"
                        'in Column Q, case sensitive.
    
                    End If
    
                End With
    
            Next Lrow
    
        End With
    
        
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    Attached Files Attached Files
    Last edited by sambuka; 06-26-2012 at 10:56 PM.

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Need VBA Code for Adding / Deleting Rows

    Can you please put you code in tags. And it would be much simpler if you could post a sample workbook to run through.

    Cheers
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Need VBA Code for Adding / Deleting Rows

    Please add code tags as per forum rule #3 and how is this thread different than here?

    Is this a duplicate of that thread?
    HTH
    Regards, Jeff

  4. #4
    Registered User
    Join Date
    03-09-2012
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Need VBA Code for Adding / Deleting Rows

    Thank you for the notice on the code tags. This new thread is a refinement on the older thread. I was able to figure out code to delete rows based on a given cell value, however I needed some help on adding and copying rows.

  5. #5
    Registered User
    Join Date
    03-09-2012
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Need VBA Code for Adding / Deleting Rows

    I have been trying to use this code to add / copy / insert row. But not working

    
    Sub INSERTROWS()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
    
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        'We use the ActiveSheet but you can replace this with
        'Sheets("MySheet")if you want
        With ActiveSheet
    
    
    
            'Set the first and last row to loop through
            Firstrow = 5
            Lastrow = 500
    
            'We loop from Lastrow to Firstrow (bottom to top)
            For Lrow = Lastrow To Firstrow Step -1
    
                'We check the values in the R column in this example
                With .Cells(Lrow, "R")
    
                    If Not IsError(.Value) Then
    
                        If .Value = "ADD" Then ActiveCell.Offset(1, 0).EntireRow.Select Selection.Insert Shift:=xlDown
                        'This will delete each row with the Value "ADD"
                        'in Column R, case sensitive.
                        
                                
                                
                    End If
    
                End With
    
            Next Lrow
    
        End With
    
        
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    
    End Sub

  6. #6
    Registered User
    Join Date
    03-09-2012
    Location
    Florida
    MS-Off Ver
    Excel 2007
    Posts
    41

    Re: Need VBA Code for Adding / Deleting Rows

    How about a code for : In a particular column (A) - if the previous cell is numeric then copy the previous row and paste insert down.

    example

    If
    A3= 1234
    A4= Text

    Then copy row A3 and paste insert down

    Can this be done?

+ 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