VBA code Copy/Paste assist

NotaVBAeXpert

New Member
Joined
May 4, 2018
Messages
26
Hi All,

So I currently have a testing document which includes testing information. I just got the code to work for inserting rows based on a particular cell directly below that row.

Now i need help figuring out how to copy certain cells from that original row into the new row and loop through my entire sample.

Example; if the row includes letter "T" in column "E", it inserts a new row directly below that line. I would then need to copy data from that row (cells "C" and "F") into the new row (cells "C & D") respectively.

this is the code that I have so far:

Sub InsertRow()


Application.ScreenUpdating = False


Dim Workbook As Workbook
Set Workbook = Workbooks("wbk")
Dim ws1 As Worksheet
Set ws1 = Workbook.Sheets("ws1")
Dim rw, i, newRow As Long
Dim lastRow As Long

' Determine last row with data in Column E
lastRow = ws1.Cells(Rows.Count, "E").End(xlUp).Row
Debug.Print lastRow

' Loop through rows in reverse order
For i = lastRow To 9 Step -1


If ws1.Cells(i, "E").Value = "F" Then
Cells(i, "E").Offset(1, 0).EntireRow.Insert Shift:=xlDown

End If

Next i
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Example; if the row includes letter "T" in column "E", it inserts a new row directly below that line.
Code:
If ws1.Cells(i, "E").Value = "[COLOR=#ff0000]F"[/COLOR] Then

despite the conflicting criteria, to copy the row with the flag in column E to the newly inserted row,
Code:
Rows(i).Copy Cells(i + 1, 1)
 
Upvote 0
Code:
If ws1.Cells(i, "E").Value = "[COLOR=#ff0000]F"[/COLOR] Then

despite the conflicting criteria, to copy the row with the flag in column E to the newly inserted row,
Code:
Rows(i).Copy Cells(i + 1, 1)


I only need to copy two cells above instead of entire row though.
 
Upvote 0
Code:
Cells(i, "C").Resize(, 2).copy Cells(i + 1, "C")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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