Insert new row if conditions are met

NotaVBAeXpert

New Member
Joined
May 4, 2018
Messages
26
Hi,

I am trying to insert a new row based on if the cells in "column E" have "B" in them. I would also like to copy the data in the originial row (Columns C and F) to the newly inserted row (columns C and F).

This is what I have so far but it isn't working and I'm not even sure if this is a good start. Any help would be appreciated.

Thanks
___________________________
Option Explicit


Sub Insert_Rows()


Dim rFound As Range, c As Range
Dim myVals
Dim i As Long

myVals = Array("B*")
Application.ScreenUpdating = False
With Range("E1", Range("E" & Rows.Count).End(xlUp))
For i = 0 To UBound(myVals)
.AutoFilter field:=1, Criteria1:=myVals(i)
On Error Resume Next
Set rFound = .Offset(1).Resize(.Rows.Count - 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilter
If Not rFound Is Nothing Then
For Each c In rFound
Rows(c.Row + 1).Insert
c.Offset(1, -1).Value = c.Value
Next c

End If
Next i
End With
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try:
Code:
Sub InsertRow()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long
    For x = LastRow To 2 Step -1
        If Cells(x, 5) Like "*B*" Then
            Rows(x + 1).Insert
            Cells(x + 1, 3) = Cells(x, 3)
            Cells(x + 1, 6) = Cells(x, 6)
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
If you are saying that the macro is not working for you, please describe how it is not working. Are you getting an error message and if so, what is the error and which line of code is highlighted when you click "Debug"? In your original post you said
"column E" have "B" in them
I interpreted this to mean that the cell may have contained other characters as well as the letter "B". If that is not the case, you can remove the "*".
 
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