[英]Excel VBA execute code in newly created workbook
I'm trying to save one worksheet in my excel file to a new file, but without the formula's. 我正在尝试将excel文件中的一个工作表保存到一个新文件中,但没有公式。
I have this code that is working to get the file saved: 我有这段代码正在工作以保存文件:
Sub SaveInvoice()
'Create a filename based on invoicenumber
Dim FileName As String
FileName = Sheets("Sale").Range("C3").Value
'Copy the "Print" sheet
Worksheets("Print").Copy
With ActiveWorkbook
'Save the file as new
.SaveAs FileName:="C:\" & FileName
End With
End Sub
This works like a charm, however I need to strip out the formula's so I googled and fount this piece of code: 这就像一个咒语,但是我需要去掉公式的,所以我用谷歌搜索并找到了这段代码:
ActiveSheet.Copy
Cells.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
And this works as well, however once I merge the two pieces of code together the whole function breaks. 这也很好,但是一旦我将这两段代码合并在一起,整个功能就会中断。
With ActiveWorkbook
'Transform cells to values
ActiveSheet.Copy
Cells.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Save the file as new
.SaveAs FileName:="C:\" & FileName
End With
This results in my base worksheet beeing stripped from formula's. 这导致我的基本工作表蜂从公式的剥离。
I need to know how I can call the function on the newle created workbook. 我需要知道如何在新创建的工作簿上调用该函数。
When copying a worksheet with no Destination
excel creates a New Workbook
, and the New Workbook
with its only Worksheet
are active. 复制没有
Destination
的工作表时,excel将创建一个New Workbook
,并且只有其Worksheet
的New Workbook
处于活动状态。
EDIT: I just realized that in the final code these lines from the original code: 编辑:我刚刚意识到,在最终代码中,这些行来自原始代码:
'Copy the "Print" sheet
Worksheets("Print").Copy
Were moved inside the 被搬进了里面
With ActiveWorkbook
That was previously referring to the New Workbook
created by the Worksheet.Copy
and that now refers to the Source Workbook
以前是指
New Workbook
Worksheet.Copy
创建的New Workbook
。 Worksheet.Copy
,现在是指Source Workbook
So let see what the Op's final code is actually doing: 因此,让我们看一下Op的最终代码到底在做什么:
Here the ActiveWorkbook
is the [Source Workbook] and the ActiveSheet
must be [Print] 这里的
ActiveWorkbook
是[源工作簿],而ActiveSheet
必须是[打印]
With ActiveWorkbook
This copies the ActiveSheet
creating a New Workbook
with only one sheet 这将复制
ActiveSheet
以仅使用一张纸来创建New Workbook
ActiveSheet.Copy
These lines are affecting the ActiveSheet
[Print] in the [Source Workbook]. 这些行影响[源工作簿]中的
ActiveSheet
[打印]。 Not because of the With
statement but because it's the one active 不是因为
With
语句,而是因为它是活动的
Cells.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
The commands within a With
intended to affect object that it refers to must start with a dot [.]; With
旨在影响所引用对象的命令必须以点[。]开头; however these lines are invalid because Cells and Range are not Methods nor Properties of the Workbook Object, thus en error would have been triggered. 但是,这些行无效,因为单元格和范围既不是方法也不是工作簿对象的属性,因此会触发错误。
With Statement
有陈述
Executes a series of statements on a single object or a user-defined type.
在单个对象或用户定义的类型上执行一系列语句。
(from the msdn.microsoft.com help)
(来自msdn.microsoft.com帮助)
This saves the Workbook referred by the With
statement, which still has the formulas 这将保存
With
语句引用的工作簿,该工作簿仍然具有以下公式:
'Save the file as new
.SaveAs FileName:="C:\" & FileName
End With
Try this code: 试试这个代码:
Sub SaveInvoice_TEST_1()
'Create a filename based on invoicenumber
Dim FileName As String
With ThisWorkbook
FileName = .Sheets("Sale").Range("C3").Value
'Copy the "Print" sheet
.Worksheets("Print").Copy
End With
With ActiveWorkbook
Rem Replace Formulas with Values
.Sheets(1).UsedRange.Value = .Sheets(1).UsedRange.Value2
'Save the file as new
.SaveAs FileName:="C:\" & FileName
End With
End Sub
Suggest to read the following pages to gain a deeper understanding of the resources used: 建议阅读以下页面,以更深入地了解所使用的资源:
Try something like the following. 尝试类似以下的方法。 Read the help page for 'ActiveSheet.Copy' - note that it creates a new workbook and activates it
阅读“ ActiveSheet.Copy”的帮助页面-注意,它将创建一个新工作簿并激活它
Dim MyWkbk as workbook
set MyWkbk = ActiveWorkbook
ActiveSheet.Copy
With ActiveWorkbook
Cells.Copy
Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Save the file as new
.SaveAs FileName:="C:\" & FileName
'.close
End With
MyWkBk.activate
Thanks to @EEM the solution was found. 感谢@EEM,找到了解决方案。
Here's the code I used: 这是我使用的代码:
Sub SaveInvoice()
Dim FileName As String
With ThisWorkbook
'Create a filename based on invoicenumber
FileName = .Sheets("Sale").Range("C3").Value
'Copy the "Print" sheet
.Worksheets("Print").Copy
End With
With ActiveWorkbook
'Replace Formulas with Values
.Sheets(1).UsedRange.Value = .Sheets(1).UsedRange.Value2
'Save the file as new
.SaveAs FileName:="C:\" & FileName
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.