Excel Macro: Copying row values from one worksheet to a specific place in another worksheet, based on criteria

Viewed 1496

I have only been working with macros in Excel for about 4 months now and have essentially been teaching myself by finding existing code and figuring out how it works. I am now a bit stuck.

I have a report in an Excel workbook. I need to copy the data across a number of worksheets (within the same workbook), based on the data that appears in column D. That is to say, I need to copy the entire row where Column D matches certain criteria. The original worksheet contains formulas, but I only want the values to appear when the data is copied.

I have been able to copy the data across, but I have two problems: 1) the formulas are copying across, not just the values 2) the data appears in the new worksheet at cell A2, but I need it to start at cell A5

I am setting this up as a template, as the main report needs to be run and split every month, so the range from which I am copying will not be constant. This is a sample of the code I am currently using:

    Sub RefreshSheets()

    Sheets("ORIGIN").Select
    Dim lr As Long, lr2 As Long, r As Long
    lr = Sheets("ORIGIN").Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = Sheets("DESTINATION").Cells(Rows.Count, "A").End(xlUp).Row

    For r = lr To 2 Step -1
        If Range("D" & r).Value = "movedata" Then
            Rows(r).Copy Destination:=Sheets("DESTINATION").Range("A" & lr2 + 1)
            lr2 = Sheets("DESTINATION").Cells(Rows.Count, "A").End(xlUp).Row
        End If


    Next r

    End Sub

I have tried adding ".PasteSpecial Paste:=xlPasteValues" after ".Range("A" & lr2 + 1)", but I get a compile error (Expected: end of statement). I am sure I have missed something obvious (this is what I get for using code I don't fully understand yet), but nothing I have tried so far has worked.

Any advice would be greatly appreciated.

2 Answers
Related