【发布时间】:2015-05-12 15:08:41
【问题描述】:
我有一个现有的 VBA 代码,它将 Excel 工作表从我的源工作簿 (Sourcewb) 复制到新的目标工作簿 (Destwb) 中,但只粘贴值。我需要 Destwb 中的特定范围 (D31:E38) 以包含源工作簿中的公式。我找到了这段代码:
Range("A1:I1105").Copy Sheets("Sheet2").Range("B2")
在这个似乎相关但不知道如何修改它以在我的应用程序中工作的网站(另一个问题)上。我添加了一个注释行“'在计算表中插入总公式”,我认为附加代码会去哪里。这是我现有的代码:
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
Sheets("Calculation").Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
FileExtStr = ".xlsx": FileFormatNum = 51
End If
End With
'Change all cells in the worksheet to values if you want
With Destwb.Sheets(1).UsedRange
Application.CutCopyMode = False
ActiveSheet.Unprotect
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
'Insert total formulas in Calc sheet
'Save the new workbook and close it
TempFilePath = Sheets("Calculation").Range("L4").Value
TempFileName = Range("L3").Value
With Destwb
.SaveAs TempFilePath & "\" & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close SaveChanges:=True
End With
MsgBox "You can find the new file in " & TempFilePath
【问题讨论】:
-
每个
Range对象都有一个名为“公式”的成员。尝试使用那个。您可能需要触摸单个单元格才能使其正常工作。