Copy based on condition

srosk

Board Regular
Joined
Sep 17, 2018
Messages
132
Hi there everyone. Hopefully an easy one :)


I have the following code which I am using on another part of my macro (which might be helpful.. I think).

Ultimately what I need to do, is copy columns A:J for any row where column L equals Not Exported. It will copy to sheet WS2, beginning on row 8. On WS2, using columns B:K (as opposed to A:J). Once copied, update value in column L with value of Exported

Is this possible in the slightest?

Code:
Function CopyToRPSTEXT()

Application.ScreenUpdating = False
Dim WS1 As Worksheet, WS2 As Worksheet, lr As Long


Set WS1 = Worksheets("2")
Set WS2 = Worksheets("3")
lr = WS1.Cells(Rows.Count, "B").End(xlUp).Row


WS1.Range("A2:J" & lr).Copy
WS2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Application.ScreenUpdating = True
Application.CutCopyMode = False


End Function
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Code:
Sub srosk()
Dim WS1 As Worksheet, WS2 As Worksheet, lr1 As Long, lr2 As Long, c As Range
Application.ScreenUpdating = False
Set WS1 = Worksheets("2")
Set WS2 = Worksheets("3")
lr1 = WS1.Cells(Rows.Count, "L").End(xlUp).Row
lr2 = 8
For Each c In WS1.Range("L1:L" & lr1)
    If c.Value = "Not Exported" Then
        WS1.Range(WS1.Cells(c.Row, "A"), WS1.Cells(c.Row, "J")).Copy
        WS2.Cells(lr2, "B").PasteSpecial Paste:=xlPasteValues
        lr2 = WS2.Cells(WS2.Rows.Count, "B").End(xlUp).Row + 1
        c.Value = "Exported"
    End If
Next c
Application.ScreenUpdating = True
Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

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