+ Reply to Thread
Results 1 to 3 of 3

Row deletion

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2011
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    2

    Row deletion

    Disclaimer: I have no idea what I am doing with VB; I work in the C family.

    Currently we have excel workbooks in which specific rows need to be deleted (skip 2, delete 3).

    I spaghetti'd this together and it seems to be working:
    Sub Change()
        Dim Cnt As Integer
        Dim DeleteFlag As Integer
        
        DeleteFlag = 1
        
        For Cnt = 1 To 505
            BgRange = "A" & Cnt & ":BB" & Cnt
                If DeleteFlag = 5 Then
                    DeleteFlag = 1
                    Range(BgRange).Interior.Color = RGB(255, 0, 0)
                    'Range("A" & Cnt).EntireRow.Delete
                Else
                    If DeleteFlag > 2 Then
                        Range(BgRange).Interior.Color = RGB(255, 0, 0)
                        'Range("A" & Cnt).EntireRow.Delete
                    End If
                    DeleteFlag = DeleteFlag + 1
                End If
        Next
    
    End Sub
    When run, this works as I want it to.

    However, when flipping statement comments (eg, comment out the BG change and uncommenting the row deletion), it gives me wildly inaccurate deletions.

    Anyone know what I am doing wrong?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,145

    Re: Row deletion

    Delete from the bottom up :-)

    For Cnt = 505 to 1 step -1

    You might need to adjust your logic ... maybe, step -5

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-12-2011
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Row deletion

    Quote Originally Posted by TMShucks View Post
    Delete from the bottom up :-)

    For Cnt = 505 to 1 step -1

    You might need to adjust your logic ... maybe, step -5

    Regards
    Excellent, thank you. I didn't realize you could do that, maybe VB isn't so horrible after all.

    For anyone who is interested, this is the working code. (I hate googling & finding these posts with no solution posted at the end.)

    Sub Change()
        Dim Row As Integer
        Dim DeleteFlag As Integer
    
        DeleteFlag = 1
        
        For Row = 505 To 1 Step -1
            bgrange = "A" & Row & ":BB" & Row
    
            If DeleteFlag > 5 Then
                DeleteFlag = 1
            End If
    
            If DeleteFlag = 4 Or DeleteFlag = 5 Then
                DeleteFlag = DeleteFlag + 1
            Else
                DeleteFlag = DeleteFlag + 1
                'Cells(Row, 2).Value = DeleteFlag
                'Range(bgrange).Interior.Color = RGB(255, 0, 0)
                Range("A" & Row).EntireRow.Delete
            End If
        Next
    
    End Sub
    Last edited by mtfd; 01-13-2011 at 11:25 AM.

+ 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