+ Reply to Thread
Results 1 to 2 of 2

Thread: If there are more than 2 blank adjacent rows, delete them

  1. #1
    Registered User
    Join Date
    06-09-2011
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    12

    If there are more than 2 blank adjacent rows, delete them

    Hello,

    I would appreciate some insight or help in getting the following pseudo code translated to VBA... I am checking all rows from ROW 1 to ROW X for blank rows. If it finds more than 2 blank rows, delete the rows until it either hits variable X or makes it 2 blank rows between data, so essentially turning this:

    *Data Rows---*
    *Blank Row*
    *Blank Row*
    *Blank Row*
    *Blank Row*
    *Blank Row*
    *Blank Row*
    *Data Rows---*
    *Blank Row*
    *Blank Row*
    *Blank Row*
    *Row X*

    into this:

    *Data Rows*
    *Blank Row*
    *Blank Row*
    *Data Rows*
    *Blank Row*
    *Blank Row*
    *Row X*

    My Brain is fried right now, and I am at my witts end... I know I must have a for loop and Row X must be subtracted by 1 every time a blank row is found and removed...

    Pseudo Code:
    For each cell in range (Row 1 to X)
    if current cell's row is empty then check if next 2 rows are empty:
    if TRUE: delete row, minus X
    if FALSE: go to next row
    Loop

    Thanks for any help, much appreciated!
    Last edited by Jon.R; 08-03-2011 at 10:51 AM.

  2. #2
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    PA
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    926

    Re: If there are more than 2 blank adjacent rows, delete them

    Hi,

    Maybe?

    Option Explicit
    Sub RowsDelete()
    Dim lrow As Long
    Dim x As Integer
    Application.ScreenUpdating = False
    lrow = Cells(Rows.Count, 1).End(xlUp).Row
        For x = lrow To 1 Step -1
            If Cells(x, 1).Value = vbNullString Then
                Cells(x, 1).EntireRow.Delete
            End If
        Next x
        For x = 1 To lrow
            If Cells(x, 1).Value <> vbNullString Then
                Range(Cells(x + 1, 1), Cells(x + 2, 1)).EntireRow.Insert
                x = x + 1
            End If
        Next x
    Application.ScreenUpdating = True
    End Sub
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

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