VBA - Copy values instead of formula

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have workbook with 2 tabs, All Data and Red Flags. I have a code that copies only the rows on the All Data tab that don't already exist on the Red Flags tab. There is a formula in column A, and I would like for to copy the values instead of the formula. Can someone help with my code below? I've tried but can't get it to paste the values for some reason.

Code:
Sub CopyRowsToSheet2()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lastRow1 As Long
    Dim lastRow2 As Long
    Dim i As Long
    Dim found As Boolean
    
    ' Set references to the worksheets
    Set ws1 = ThisWorkbook.Sheets("All Data")
    Set ws2 = ThisWorkbook.Sheets("Red Flags")
    
    ' Find the last row in each sheet
    lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
    lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
    
    ' Loop through each row in Sheet1
    For i = 2 To lastRow1 ' Assuming data starts from row 2
        
        ' Reset the found flag
        found = False
        
        ' Check if the row already exists in Sheet2
        For j = 2 To lastRow2
            If ws1.Cells(i, "A").Value = ws2.Cells(j, "A").Value Then
                found = True
                Exit For
            End If
        Next j
        
        ' If the row doesn't exist, copy it to Sheet2
        If Not found Then
            lastRow2 = lastRow2 + 1 ' Increment last row in Sheet2
            ws1.Rows(i).Copy Destination:=ws2.Rows(lastRow2)
        End If
    Next i
    
    ' Cleanup
    Set ws1 = Nothing
    Set ws2 = Nothing
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You have a couple of options with the first being the preference:
VBA Code:
'opton 1
ws2.Rows(lastRow2).Value = ws1.Rows(i).Value

'option 2
ws1.Rows(i).Copy
ws2.Rows(lastRow2).PasteSpecial xlValues
 
Upvote 0
Solution
You have a couple of options with the first being the preference:
VBA Code:
'opton 1
ws2.Rows(lastRow2).Value = ws1.Rows(i).Value

'option 2
ws1.Rows(i).Copy
ws2.Rows(lastRow2).PasteSpecial xlValues
Thank you for the quick response. Option 1 is perfect. I appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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