Win32的Excel复制除了功能

时间:2018-12-06 01:48:36

标签: python excel winapi win32com

我在python中使用win32com进行excel控制,并且遇到了一些问题。

我想将我的Excel单元格(1,1)即B2复制到“ A1”

Excel Worksheet

下面是我的代码

Sheet.Range(Sheet.Cells(1,1), Sheet.Cells(LastRow, Lastcol)).copy(Sheet1.Range("A1"))

它可以正常工作,但是它复制公式而不是公式值(结果)。 (我希望“ 0”不是“ = SUM(C1,C2)”))

请指导,如何更改代码以获得所需的结果?

1 个答案:

答案 0 :(得分:0)

这可能是由于目标单元格格式不正确造成的。因此,您的代码需要更改为以下内容。我建议将您现有的行替换为以下内容:

'Change the format of cell to General as Text fields do not evaluate the formula
Sheet.Range(Sheet.Cells(1,1), Sheet.Cells(LastRow, Lastcol)).NumberFormat = "General"

'Copy the formula and assign it as formula (rather than copying it)
Sheet.Range(Sheet.Cells(1,1), Sheet.Cells(LastRow, Lastcol)).Formula  = Sheet1.Range("A1").Formula

希望有帮助!