Remove random blanks and add alternate empty rows.

topi1

Active Member
Joined
Aug 6, 2014
Messages
252
Office Version
  1. 2010
Hi, Can someone please help me with following. Need a macro to remove random blank rows in the column R in the sheet “sheet2”. Once the blank rows removed, add alternate blank rows so that the final result is non blank rows separated by a single blank row. Thank you,
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
If it is not too time consuming, can you please provide both? Thanks.
 
Upvote 0
e.g.
VBA Code:
Sub RemoveAndInsertRows()
    Dim rngUnion As Range
    Dim rngSource As Range
    Dim lRow As Long
    Dim rngCell As Range
    Dim i As Long

    lRow = GetLastCell(ActiveSheet.UsedRange, xlByRows).Row

    Set rngSource = Range("R1", Cells(lRow, "R"))

    For Each rngCell In rngSource.Cells
        If IsEmpty(rngCell.Value) Then
            If Not rngUnion Is Nothing Then
                Set rngUnion = Union(rngUnion, rngCell)
            Else
                Set rngUnion = rngCell
            End If
        End If
    Next rngCell

    If Not rngUnion Is Nothing Then
        rngUnion.EntireRow.Delete
    End If


    For i = rngSource.Rows.Count To 2 Step -1
        Rows(i).Insert
    Next i

End Sub


Function GetLastCell(InRange As Range, SearchOrder As XlSearchOrder, _
                     Optional ProhibitEmptyFormula As Boolean = False) As Range
    ' By Chip Pearson,  www.cpearson.com
    Dim WS As Worksheet
    Dim R As Range
    Dim LastCell As Range
    Dim LastR As Range
    Dim LastC As Range
    Dim SearchRange As Range
    Dim LookIn As XlFindLookIn
    Dim RR As Range

    Set WS = InRange.Worksheet

    If ProhibitEmptyFormula = False Then
        LookIn = xlFormulas
    Else
        LookIn = xlValues
    End If

    Select Case SearchOrder
        Case XlSearchOrder.xlByColumns, XlSearchOrder.xlByRows, _
             XlSearchOrder.xlByColumns + XlSearchOrder.xlByRows
            ' OK
        Case Else
            Err.Raise 5
            Exit Function
    End Select

    With WS
        If InRange.Cells.Count = 1 Then
            Set RR = .UsedRange
        Else
            Set RR = InRange
        End If

        Set R = RR(RR.Cells.Count)

        If SearchOrder = xlByColumns Then
            Set LastCell = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                   LookAt:=xlPart, SearchOrder:=xlByColumns, _
                                   SearchDirection:=xlPrevious, MatchCase:=False)
        ElseIf SearchOrder = xlByRows Then
            Set LastCell = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                   LookAt:=xlPart, SearchOrder:=xlByRows, _
                                   SearchDirection:=xlPrevious, MatchCase:=False)
        ElseIf SearchOrder = xlByColumns + xlByRows Then
            Set LastC = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                LookAt:=xlPart, SearchOrder:=xlByColumns, _
                                SearchDirection:=xlPrevious, MatchCase:=False)
            Set LastR = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                LookAt:=xlPart, SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, MatchCase:=False)
            Set LastCell = Application.Intersect(LastR.EntireRow, LastC.EntireColumn)
        Else
            Err.Raise 5
            Exit Function
        End If
    End With

    Set GetLastCell = LastCell

End Function
Artik
 
Upvote 0
e.g.
VBA Code:
Sub RemoveAndInsertRows()
    Dim rngUnion As Range
    Dim rngSource As Range
    Dim lRow As Long
    Dim rngCell As Range
    Dim i As Long

    lRow = GetLastCell(ActiveSheet.UsedRange, xlByRows).Row

    Set rngSource = Range("R1", Cells(lRow, "R"))

    For Each rngCell In rngSource.Cells
        If IsEmpty(rngCell.Value) Then
            If Not rngUnion Is Nothing Then
                Set rngUnion = Union(rngUnion, rngCell)
            Else
                Set rngUnion = rngCell
            End If
        End If
    Next rngCell

    If Not rngUnion Is Nothing Then
        rngUnion.EntireRow.Delete
    End If


    For i = rngSource.Rows.Count To 2 Step -1
        Rows(i).Insert
    Next i

End Sub


Function GetLastCell(InRange As Range, SearchOrder As XlSearchOrder, _
                     Optional ProhibitEmptyFormula As Boolean = False) As Range
    ' By Chip Pearson,  www.cpearson.com
    Dim WS As Worksheet
    Dim R As Range
    Dim LastCell As Range
    Dim LastR As Range
    Dim LastC As Range
    Dim SearchRange As Range
    Dim LookIn As XlFindLookIn
    Dim RR As Range

    Set WS = InRange.Worksheet

    If ProhibitEmptyFormula = False Then
        LookIn = xlFormulas
    Else
        LookIn = xlValues
    End If

    Select Case SearchOrder
        Case XlSearchOrder.xlByColumns, XlSearchOrder.xlByRows, _
             XlSearchOrder.xlByColumns + XlSearchOrder.xlByRows
            ' OK
        Case Else
            Err.Raise 5
            Exit Function
    End Select

    With WS
        If InRange.Cells.Count = 1 Then
            Set RR = .UsedRange
        Else
            Set RR = InRange
        End If

        Set R = RR(RR.Cells.Count)

        If SearchOrder = xlByColumns Then
            Set LastCell = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                   LookAt:=xlPart, SearchOrder:=xlByColumns, _
                                   SearchDirection:=xlPrevious, MatchCase:=False)
        ElseIf SearchOrder = xlByRows Then
            Set LastCell = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                   LookAt:=xlPart, SearchOrder:=xlByRows, _
                                   SearchDirection:=xlPrevious, MatchCase:=False)
        ElseIf SearchOrder = xlByColumns + xlByRows Then
            Set LastC = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                LookAt:=xlPart, SearchOrder:=xlByColumns, _
                                SearchDirection:=xlPrevious, MatchCase:=False)
            Set LastR = RR.Find(what:="*", after:=R, LookIn:=LookIn, _
                                LookAt:=xlPart, SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, MatchCase:=False)
            Set LastCell = Application.Intersect(LastR.EntireRow, LastC.EntireColumn)
        Else
            Err.Raise 5
            Exit Function
        End If
    End With

    Set GetLastCell = LastCell

End Function
Artik
Thank you so much. It works but sometimes it is erratic and does not clean up between last two entries. For example, see the large gap between h and i in the following example. TY so much.

Book1
K
1a
2
3b
4
5c
6
73
8
94
10
11d
12
135
14
15e
16
176
18
19g
20
21h
22
23
24
25
26
27i
Sheet2
 
Upvote 0
The image you attached shows data in column K, while my code operates on data in column R.

Artik
 
Upvote 0
Thank you so much. It works but sometimes it is erratic and does not clean up between last two entries. For example, see the large gap between h and i in the following example. TY so much.
The image you attached shows data in column K, while my code operates on data in column R.

Artik
Sorry for the confusion. I ran the code in 365 but had not activated XL2Bb so copied in to 2010 version and sent. Sorry for the confusion.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top