win32 excel 复制除了函数

win32 excel copy except function

我在 python 中使用 win32com 进行 excel 控制并遇到一个小问题。

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

下面是我的代码

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

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

请指导,我应该如何更改我的代码才能获得所需的结果?

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

'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

希望对您有所帮助!