简体   繁体   中英

What is proper way of using xlPasteSpecial.Values?

I need to remove formulas from a range of cells so that only values are kept.

I am using Excel.Interop

I've recorded VBA macro (simply copying all and pasting the special values) and did the same in C# with following code:

xlWorkSheet.Activate();
    xlWorkSheet.Range["A:Z"].Copy();
    xlWorkSheet.UsedRange.ClearContents();
    xlWorkSheet.Range["A:Z"].Select();
    xlWorkSheet.Range["A:Z"].PasteSpecial(
       Excel.XlPasteType.xlPasteValues,
       Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,
       System.Type.Missing, System.Type.Missing);

However, I'm receiving the following error:

System.Runtime.InteropServices.COMException: 'PasteSpecial method of Range class failed'

Figured out :

    xlWorkSheet.Activate();
    xlWorkSheet.Range["A:Z"].Select();
    xlWorkSheet.UsedRange.Copy();
    xlWorkSheet.Range["A:Z"].PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, System.Type.Missing, System.Type.Missing);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM