Copy & Paste based on conditions

BlairClark

New Member
Joined
Mar 23, 2019
Messages
4
Hi,
I'm trying to create a macro that looks up the word "Insert" in column E2, copies the contents from A2:G20 and pastes in the row directly below (A3:G21). I'm hoping to loop the macro because there's the possibility that more rows need the same argument.
[TABLE="class: grid, width: 300"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]SKU[/TD]
[TD]Description[/TD]
[TD]Start[/TD]
[TD]End[/TD]
[TD]Comments[/TD]
[TD]Hours[/TD]
[TD]Cases[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1001[/TD]
[TD]LB 12x330[/TD]
[TD]25/03/19 05:30[/TD]
[TD]26/03/19 02:30[/TD]
[TD]Insert[/TD]
[TD][/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1003[/TD]
[TD]LB 24x330[/TD]
[TD]26/03/19 03:30[/TD]
[TD]26/03/19 22:00[/TD]
[TD][/TD]
[TD][/TD]
[TD]2579[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]1010[/TD]
[TD]LR 12x330[/TD]
[TD]26/03/19 23:00[/TD]
[TD]27/03/19 19:30[/TD]
[TD]Insert[/TD]
[TD][/TD]
[TD]5000[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]1011[/TD]
[TD]LR 24x330[/TD]
[TD]27/03/19 20:30[/TD]
[TD]28/03/19 16:00[/TD]
[TD]Insert[/TD]
[TD][/TD]
[TD]5000[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]1009[/TD]
[TD]ST 12x330[/TD]
[TD]28/03/19 17:00[/TD]
[TD]28/03/19 23:00[/TD]
[TD][/TD]
[TD][/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]1008[/TD]
[TD]ST 24x330[/TD]
[TD]29/03/19 00:00[/TD]
[TD]29/03/19 20:00[/TD]
[TD][/TD]
[TD][/TD]
[TD]4000[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]1007[/TD]
[TD]SP 12x330[/TD]
[TD]29/03/19 21:00[/TD]
[TD]30/03/19 04:00[/TD]
[TD]Insert[/TD]
[TD][/TD]
[TD]3790[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]1006[/TD]
[TD]SP 24x330[/TD]
[TD]30/03/19 05:00[/TD]
[TD]30/03/19 18:00[/TD]
[TD][/TD]
[TD][/TD]
[TD]3330[/TD]
[/TR]
</tbody>[/TABLE]

Any help would be appreciated.

Blair
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,

You can test following macro :

Code:
Sub CopyRowsWithCondition()
' If Cell in E Column = Insert or Copy
Dim i As Long
Dim arCrit As Variant
Dim arVal As Variant


Application.DisplayAlerts = False
arCrit = Array("Insert", "Copy")
' Adjust your tab Name
With Worksheets("Sheet2")
  For i = 100 To 2 Step -1
    For Each arVal In arCrit
      If InStr(1, .Range("E" & i).Value, arVal) >= 1 Then
          .Rows(i + 1).Insert
          .Range("A" & i & ":G" & i).Copy Destination:=Range("A" & i + 1)
      End If
    Next arVal
  Next i
End With
Application.DisplayAlerts = True


End Sub

Hope this will help
 
Upvote 0
Thanks James006 (should be 007).
This works brilliantly.

One more question... Is there a way once the above macro has been run to change the dates in column C & D. This is a production schedule and I need to find out how much of something we produce on any given day. As you can see, all the insert rows are items that run over multiple days i.e. Row 2 - 25/03/19 05:30 to 26/03/19 02:30. Once your macro runs I need to change the original end to represent the start date but for a time prior to midnight (End will become 25/03/19 23:59 in Row 2). The inserted row will in turn have a different start time - 26/03/19 00:00.

Is this possible?
 
Upvote 0
Glad to hear macro is helping you out ... :smile:

What are precisely the rules for Start and End fields for any newly inserted row ???
 
Upvote 0
Every copied row needs the end (Column D) changed to 23:59 on the same date as the start column.
Every inserted row needs the start (Column C) changed to 00:00 on the same date as the end column.

Original example below:
A2 - 1001
B2 - LB 12x330
C2 - 25/03/19 05:30D3 - 26/03/19 02:30
E2 - Insert
F2 -
G2 - 3000

Requirements after CopyRowsWithCondition macro
A2 - 1001
B2 - LB 12x330
C2 - 25/03/19 05:30
D2 - 25/03/19 23:59

E2 - Insert
F2 -
G2 - 3000

A3 - 1001
B3 - LB 12x330
C3 - 26/03/19 00:00
D3 - 26/03/19 02:30
E3 - Insert
F3 -
G3 - 3000

I hope this makes sense?
 
Upvote 0
Brilliant Explanation ... !!! :smile:

Are you a Teacher or an Instructor ... ???

If not ... it is a shame ... you do have a real talent for exposing issues ... 8-)
 
Upvote 0
Thanks to your Great explanation ....

the modiifications were dead simple to implement ...

Code:
Sub CopyRowsWithCondition()
' If Cell in E Column = Insert or Copy
Dim i As Long
Dim arCrit As Variant
Dim arVal As Variant


arCrit = Array("Insert", "Copy")
' Adjust your tab Name
With Worksheets("Sheet1")
  For i = 10 To 2 Step -1
    For Each arVal In arCrit
      If InStr(1, .Range("E" & i).Value, arVal) >= 1 Then
          .Rows(i + 1).Insert
          .Range("A" & i & ":G" & i).Copy Destination:=Range("A" & i + 1)
          ' Copied is identified with i
          .Range("D" & i).Value = Int(.Range("C" & i).Value) + 0.999305
          ' Inserted is identified with i + 1
          .Range("C" & i + 1).Value = Int(.Range("D" & i + 1).Value)
      End If
    Next arVal
  Next i
End With


End Sub

Hope this in line with your expectations ...

P.S. by the way ... don't you need the E Column to be completely cleared of the "Insert" comment ... once the macro is over ... ???
 
Last edited:
Upvote 0
Wow!

You've exceeded my expectations... this works brilliantly. I wish I was at the level you're at... clearing the "insert" is right up my alley :)

There's potentially more to come too... for now, you've been a lifesaver.

For a brewery scheduler this is magic. Thanks heaps @James006.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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