Paste cells before or after certain rows

topi1

Board Regular
Joined
Aug 6, 2014
Messages
190
Office Version
  1. 2010
I was hoping to get a macro for the following two-part function. Thank you.
Trying to copy and paste Based on first occurrence in the cells.
The data is in sheet2.
Hoping for case-insensitive match.

Part I
When the cells below TITLE2 start with "After" or "Below", want the rows to be copied and pasted above TITLE2 row, below the row where column O cell is identical.

Part II
When the cells below TITLE2 start with "Before" or "Above", want the rows to be copied and pasted above TITLE2 row, above the row where column O cell is identical.

I can use formulas, but I would like to use VBA if possible.

Here is the example

Before:

Book3
OPQR
1Theater:
2Movie:
3
4
5TITLE1
6
7ErosEros:4Eros: Dunn
8RegalRegal:4Regal: Superman
9MetroMetro:4Metro: Batman
10MinervaMinerva:4Minerva: Starwars
11
12
13TITLE2
14Eros shows Superman nextweek. Delim4.
15Metro shows Dunn next month. Delim3. Closed today.
16Delim2. Minerva is showing Abba.
17MetroPlace4Before Metro Roxi shows Cars.
18ErosPlace4After Eros Strand shows Abba.
19ErosPlace4After Eros Mandir shows Sholay
20MetroPlace4Before Metro Opera House shows Shor
Sheet2


After:

Book3
OPQR
1Theater:
2Movie:
3
4
5TITLE1
6
7ErosEros:4Eros: Dunn
8ErosPlace4After Eros Strand shows Abba.
9ErosPlace4After Eros Mandir shows Sholay
10RegalRegal:4Regal: Superman
11MetroPlace4Before Metro Roxi shows Cars.
12MetroPlace4Before Metro Opera House shows Shor
13MetroMetro:4Metro: Batman
14MinervaMinerva:4Minerva: Starwars
15
16
17TITLE2
18Eros shows Superman nextweek. Delim4.
19Metro shows Dunn next month. Delim3. Closed today.
20Delim2. Minerva is showing Abba.
21MetroPlace4Before Metro Roxi shows Cars.
22ErosPlace4After Eros Strand shows Abba.
23ErosPlace4After Eros Mandir shows Sholay
24MetroPlace4Before Metro Opera House shows Shor
Sheet2
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try:
VBA Code:
Sub FindCopySort()
    Dim ws As Worksheet
    Dim startCellR As Range
    Dim lastRow As Long
    Dim firstNonBlankO As Long
    Dim title2Range As Variant
    Dim result() As Variant
    Dim i As Long, j As Long, k As Long

    On Error Resume Next
    Set ws = ThisWorkbook.Sheets("Sheet2")
    On Error GoTo 0
    

    Dim startCellRTitle1 As Range
    Set startCellRTitle1 = ws.Columns("R").Find(What:="TITLE1", LookIn:=xlValues, LookAt:=xlWhole)
    If startCellRTitle1 Is Nothing Then Exit Sub

    Set startCellR = ws.Columns("R").Find(What:="TITLE2", LookIn:=xlValues, LookAt:=xlWhole)
    If startCellR Is Nothing Then Exit Sub

    lastRow = ws.Cells(ws.Rows.Count, "R").End(xlUp).Row
    If lastRow < startCellR.Row Then Exit Sub
    
    title2Range = ws.Range(startCellR.Offset(0, -3), ws.Cells(lastRow, "R")).Value
    firstNonBlankO = ws.Columns("O").SpecialCells(xlCellTypeConstants).Cells(1, 1).Row
    If firstNonBlankO = 0 Then Exit Sub
    
    ReDim result(1 To UBound(title2Range, 1), 1 To 4)
    j = 1
    For i = 2 To UBound(title2Range, 1)
        If InStr(1, title2Range(i, 4), "After", vbTextCompare) > 0 Or _
           InStr(1, title2Range(i, 4), "Before", vbTextCompare) > 0 Then
            For k = 1 To 4
                result(j, k) = title2Range(i, k)
            Next k
            j = j + 1
        End If
    Next i
    
    If j > 1 Then
        ws.Rows(firstNonBlankO + 1 & ":" & firstNonBlankO + UBound(result, 1)).Insert Shift:=xlDown
        ws.Range("O" & firstNonBlankO + 1).Resize(UBound(result, 1), 4).Value = result

        Dim sortRng As Range
        Set sortRng = ws.Range(ws.Cells(startCellRTitle1.Row + 1, "O"), ws.Cells(startCellR.Row - 1, "R"))
        sortRng.Sort Key1:=ws.Range("O" & startCellRTitle1.Row + 1), Order1:=xlAscending, Header:=xlNo
        
        Dim delRange As Range
        For Each cell In ws.Range(ws.Cells(startCellRTitle1.Row + 1, "R"), ws.Cells(startCellR.Row - 1, "R"))
            If IsEmpty(cell) Then
                If delRange Is Nothing Then
                    Set delRange = cell
                Else
                    Set delRange = Union(delRange, cell)
                End If
            End If
        Next cell
        If Not delRange Is Nothing Then delRange.EntireRow.Delete
    End If
End Sub
 
Upvote 0
Solution
@Cubist. Thank you. It worked like a charm. Had been struggling with it. Mine was only doing one instance of "After". This one is perfect.
 
Upvote 0

Forum statistics

Threads
1,216,499
Messages
6,131,012
Members
449,613
Latest member
MedDash99

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