2
votes

I need to copy rows between multiple worksheets and while I managed to do that it only copies the formulas and not the values. I haven't used VBA for years and I just can't think of how to do it.

My existing code is:

Workbooks.Open filename:=NewFN
    filename = Mid(NewFN, InStrRev(NewFN, "\") + 1)


        For i = 1 To 14
            Workbooks(filename).Sheets("sheet1").Rows(i).Copy ThisWorkbook.Sheets("BD Raw Data").Rows(insertRow)

            insertRow = insertRow + 1
        Next i

        Workbooks(filename).Close SaveChanges:=False
3
Two things (1) why use a loop when you could copy the 14 rows in one shot. (2) You don't need to use Copy, you can replicate values by using rng1.Value = rng2.Value where rng2 is a source range and rng1 is a destination 9ranges of equal size). To copy formula rng1.Formula' = rng2.Formula` - brettdj

3 Answers

2
votes

You need to use the PasteSpecial method using the XlPasteType.xlPasteValues enumeration instead of Copy Destination

e.g.

Workbooks(filename).Sheets("sheet1").Rows(i).Copy 
ThisWorkbook.Sheets("BD Raw Data").Rows(insertRow).PasteSpecial Operation:=xlPasteValues
1
votes

The choice between values and formulas is made in the paste half of the process. Have a look at the PasteSpecial method specifically the xlPasteValues argument.

http://msdn.microsoft.com/en-us/library/aa195818(v=office.11).aspx

-2
votes

Try it

 Sub CopyFormulas()
'http://officevb.com

Dim sht1 As Worksheet, sht2 As Worksheet

Set sht1 = Sheets("Sheet1")
Set sht2 = Sheets("sheet2")

'copy columns C from sht1 formulas to sht2

sht1.Range("C:C").Copy
sht2.Range("C:C").PasteSpecial xlPasteFormulas

Application.CutCopyMode = False

End Sub

This sub copy all formulas in column C from sht1 to column C in sht2

[]´s