How to copy and paste data with values only when a condition is met?

kev_bez

New Member
Joined
Jul 20, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Good afternoon all, I have this code below which is copying and pasting data from sheet3 to sheet 47 when column F has either "RI" or "Issue" written in it. The code works but it is also pasting any formulas and comments I have in the data. Is there a way to make this work with values only?

Thank you!

Sub Live_CWO()
Dim StatusCol As Range
Dim Status As Range
Dim Pastecell As Range

Set StatusCol = Sheet3.Range("F4:F50")
For Each Status In StatusCol

If Sheet47.Range("A2") = "" Then
Set Pastecell = Sheet47.Range("A2")
Else
Set Pastecell = Sheet47.Range("A1").End(xlDown).Offset(1, 0)

End If

If Status = "RI" Then Status.Offset(0, -2).Resize(1, 5).Copy Pastecell
If Status = "ISSUE" Then Status.Offset(0, -2).Resize(1, 5).Copy Pastecell


Next Status

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Typically, you would avoid that by setting the cell values directly equal to one another instead of using Copy/Paste, i.e.
Range(destination cell).Value = Range(source cell).Value

If you need help modifying your code, please an example of your source data and expected result, so we can see what everything looks like.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Good morning, apologies for the extremely late reply. I managed to install XL2BB. What part of the code would I replace?
I want the code to look in column F of Sheet 3 if any of the cells have the text "ISSUE" OR "RI" and if condition is true then to copy columns D,E,F,G,H of that paticular row and paste onto a seperate sheet called Live CWO (Sheet 47) but I need as values only.
The code works at the moment but it copies the formulas from columns G & H rather than just the values.

Thank you!

UNDERBODY SQUAD Wk40.xlsm
ADEFGHIJKLMNOP
1PRINT WO (ENTER "Y")z8.1 - ALL SAFETY AREAS
2
38.1FaultCCAR # OwnerOccDTWed PMWed NThu AMThu PMThu NFri AMFri PMFri N
402: * Rob_In[4,0].4 100R01 do5 DCP FAULT224RI32.251.653.020.0021.329.040.000.009.02
504: . _005S502 TROLLEY STOP205RI287.844.138.847.730.002.863.950.0019.49
610: . _071S513 LH Front Baffle CheckCWO_83 iROB CCARESCALATE21.391.3820.3916.3319.0814.776.207.8310.38
719: . _300S222 ST300 PALLET IN POSITION 2213RI526.280.9410.282.843.645.205.841.8010.08
815: * Rob_In[11,0].4 040R04 do5 DCP FAULTCWO_94RI39.972.8621.629.697.7519.790.000.000.00
912: . _EZ81_034.SA07_OK SA07 Safety Area Reset RequiredCWO_2 CCAR 33ESCALATE521.9522.303.942.410.0012.779.095.230.59
1009: * Rob_In[18,0].4 250R09 do5 DCP FAULT208CLOSED00.000.000.0016.3920.1946.700.000.000.00
1106: * Rob_In[12,0].4 230R05 do5 DCP FAULT210CLOSED00.009.780.000.007.5535.5641.330.000.00
1201: . Rob_In[2,0].11 210R02 do12 ON PATH00.003.470.371.660.747.701.170.003.36
1313: . Rob_In[12,0].0 080R04 do1 DRIVES ON209RI00.003.214.7212.342.724.360.002.027.55
1406: * Rob_In[10,0].4 230R01 do5 DCP FAULTCCAR 17D.Greenwell S.Ansell00.000.001.796.060.005.831.342.942.84
1519: * Rob_In[16,0].4 300R06 do5 DCP FAULT135MON924.720.000.240.000.000.007.100.003.78
1613: . _SA07_Safety_Area_OK _SA07 Safety Area / Maint Zone + Gates OKCWO_3 CCAR 33ESCALATE00.0022.303.940.820.0011.130.000.000.59
1707: * Rob_In[16,0].17 240R05 do18 AREA RELEASE 2 RIVET CAR238ISSUE00.001.648.332.022.281.071.410.960.88
1802: . _EZ65_035.Estops_OK EZ65_035 PLC ESTOP OK336.010.000.000.000.000.000.000.000.00
1907: * Rob_In[15,0].4 240R02 do5 DCP FAULT225MON13.840.834.813.681.690.002.0119.310.00
2013: * Rob_In[12,0].4 080R04 do5 DCP FAULT239ISSUE22.570.8210.794.104.070.580.007.927.66
2101: * Rob_In[1,0].4 090R01 do5 DCP FAULT157MON00.000.000.000.0010.700.005.235.2810.16
2202: * Rob_In[5,0].4 100R02 do5 DCP FAULT173MON11.120.7811.182.460.002.780.006.6116.36
2305: * Rob_In[15,0].4 120R05 do5 DCP FAULT187CLOSED00.0024.3126.152.530.000.000.000.000.00
2408: * Robot2.ZSF3_Err 430R01 SAFEMOVE ZONE 3 VIOLATED13.320.665.870.009.260.273.910.007.61
2508: * Rob_In[6,0].4 070R03 do5 DCP FAULT11.420.000.000.000.000.000.000.000.00
2605: * Rob_In[14,0].4 120R02 do5 DCP FAULT00.000.000.000.001.790.000.000.000.00
z8.1
Cell Formulas
RangeFormula
G4:G26G4=IFERROR(XLOOKUP(D4,'Ref 8.1'!E:E,'Ref 8.1'!B:B,0),0)
H4:H26H4=IFERROR(XLOOKUP(D4,'Ref 8.1'!E:E,'Ref 8.1'!C:C,0),0)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D997:D1048576,D1:D833Cell ValueduplicatestextNO
D1:D814,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D808,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D794,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D786,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D784,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D769,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D767,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D764,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D762,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D757,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D751,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D737,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D732,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D731,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D723,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D722,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D716,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D713,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D711,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D708,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D700,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D699,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D692,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D691,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D681,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D679,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D671,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D668,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D666,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D659,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D651,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D637,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D633,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D624,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D617,D997:D1048576,D823:D833Cell ValueduplicatestextNO
G1:H2Cell ValueduplicatestextNO
D1Cell ValueduplicatestextNO
D1,D3:D348,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D340,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D60,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D89,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D127,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D187,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D199,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D213,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D223,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D234,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D245,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D251,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D263,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D269,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D279,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D296,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D302,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D307,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D312,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D319,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D331,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D3:E3,D997:E1048576,D823:E833,E4,E5:F5,E6:E800,E834:E996,E802:E822Cell ValueduplicatestextNO
D1,D3:D360,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D368,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D381,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D390,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D398,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D415,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1,D3:D425,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D428,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D434,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D440,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D462,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D467,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D476,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D488,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D495,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D498,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D503,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D508,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D511,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D515,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D518,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D527,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D592,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D592,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D604,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D607,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D1:D612,D997:D1048576,D823:D833Cell ValueduplicatestextNO
D4:D36Cell ValueduplicatestextYES
 
Last edited:
Upvote 0
copy columns D,E,F,G,H of that paticular row and paste onto a seperate sheet called Live CWO (Sheet 47) but I need as values only.
Can you post what your destination sheet (Live CWO, Sheet 47) looks like, so we can see the structure of that?
 
Upvote 0
Currently it is like this but It is open to be modified to suit.
Can you post what your destination sheet (Live CWO, Sheet 47) looks like, so we can see the structure of that?

UNDERBODY SQUAD Wk40.xlsm
ABCDEFGHIJK
1
2UB
3NoZoneDT/OccOwnerReturnedNoZoneDT/OccOwnerReturned
4
5
6
7
8
9
10
11
12
13
14
15
16
17FRMFRM
18NoZoneDT/OccOwnerReturnedNoZoneDT/OccOwnerReturned
19
20
21
22
23
24
25
26
27
28
29
30
31
32CLOCLO
33NoZoneDT/OccOwnerReturnedNoZoneDT/OccOwnerReturned
34
35
36
37
38
39
40
41
42
Sh
 
Upvote 0
Try changing this part of your code:
VBA Code:
If Status = "RI" Then Status.Offset(0, -2).Resize(1, 5).Copy Pastecell
If Status = "ISSUE" Then Status.Offset(0, -2).Resize(1, 5).Copy Pastecell
to this:
VBA Code:
If Status = "RI" Then
    Status.Offset(0, -2).Resize(1, 5).Copy
    Pastecell.PasteSpecial Paste:=xlPasteValues
End If

If Status = "ISSUE" Then
    Status.Offset(0, -2).Resize(1, 5).Copy
    Pastecell.PasteSpecial Paste:=xlPasteValues
End If
 
Upvote 1
Solution
Try changing this part of your code:
VBA Code:
If Status = "RI" Then Status.Offset(0, -2).Resize(1, 5).Copy Pastecell
If Status = "ISSUE" Then Status.Offset(0, -2).Resize(1, 5).Copy Pastecell
to this:
VBA Code:
If Status = "RI" Then
    Status.Offset(0, -2).Resize(1, 5).Copy
    Pastecell.PasteSpecial Paste:=xlPasteValues
End If

If Status = "ISSUE" Then
    Status.Offset(0, -2).Resize(1, 5).Copy
    Pastecell.PasteSpecial Paste:=xlPasteValues
End If
That has worked perfectly! Thank you ver much! Appreciate you taking time to help me out!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 1

Forum statistics

Threads
1,215,078
Messages
6,122,996
Members
449,093
Latest member
masterms

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