VBA Help!!! Paste Special Value based on Existing Code

kenchristensen11

Board Regular
Joined
Oct 12, 2016
Messages
52
Hello,

I have the below code but after it copies the entire row to the target sheet, the formulas are still there.

What I need to it do is Paste them as values (to get rid of the formula) after it's copied to the target sheet, BUT also keep formatting the same from the source sheet.


VBA Code:
Sub MoveRowsToArchive()
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Long
Dim i As Long

' Set the source and target sheets
Set sourceSheet = ThisWorkbook.Worksheets("Quarterly Prep")
Set targetSheet = ThisWorkbook.Worksheets("Archive")

' Find the last row in the source sheet
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "B").End(xlUp).Row

' Loop through each row in the source sheet
For i = 9 To lastRow
' Check if cell in column X contains "Archive"
If sourceSheet.Cells(i, "B").Value = "Archive" Then
' Copy the entire row to the target sheet
sourceSheet.Rows(i).Copy Destination:=targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Offset(1)
' Delete the row from the source sheet
sourceSheet.Rows(i).Delete
' Decrement the loop counter as the rows are shifting up
i = i - 1
' Update the last row value
lastRow = lastRow - 1
End If
Next i
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Replace the one line that currently does the copy/paste with these lines. Cannot test without your file.

VBA Code:
sourceSheet.Rows(i).Copy
targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = xlCopy
 
Upvote 0
Replace the one line that currently does the copy/paste with these lines. Cannot test without your file.

VBA Code:
sourceSheet.Rows(i).Copy
targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = xlCopy
Thank you. Is there any way where instead of copying the ENTIRE row from the source sheet, it selects the data in the row from columns B to I only if a value in column B = "Archive" from the source sheet to the target sheet, starting in cell B5 in the target sheet?
 
Upvote 0
a
Thank you. Is there any way where instead of copying the ENTIRE row from the source sheet, it selects the data in the row from columns B to I only if a value in column B = "Archive" from the source sheet to the target sheet, starting in cell B5 in the target sheet?
Add a code that filters column B based on your criteria before the past as value code. I'd use a helper column filtering 1 or 0 probably on column A if its not occupied with any. Hope it helps.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

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