3

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
1
  • 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` Commented Jan 25, 2012 at 23:50

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately this produces the error code 1004 PasteSpecial method of Range class failed
Should change Operation:= to Paste:= in second line. Worked for me.
1

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

Comments

-2

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

1 Comment

Unfortunately the number of columns needed to be copied varies depending on which worksheet is opened meaning a column copying solution wouldn't be the right fit for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.