简体   繁体   English

Excel VBA在新创建的工作簿中执行代码

[英]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 ,并且只有其WorksheetNew 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 WorkbookWorksheet.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: 建议阅读以下页面,以更深入地了解所使用的资源:

With Statement 有陈述

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.

相关问题 我如何设计 VBA 中的代码,以便在 Excel 中引用以前新创建的工作簿? - How can I design the code in VBA in order to refer to a previously already newly created workbook in Excel? Excel VBA-SaveCopyAs,然后将活动工作簿更改为新创建的文件 - Excel VBA-SaveCopyAs and then change active workbook to newly created file 节省新创建的VBA工作簿 - Trouble Saving Newly Created VBA Workbook Excel Addin - 更新新建工作簿的数据 - Excel Addin - update data of newly created workbook Excel VBA从目录中的源中提取数据并合并到新创建的工作簿中 - Excel VBA pulled data from source in directory and merge into newly created workbook 如何将新创建的Excel WOrkBook打开为活动工作簿 - How to Open a newly Created Excel WOrkBook as Active Workbook 如何在VBA中将输入表单复制到新创建的工作簿 - How to copy an input form to a newly created workbook in VBA VBA/Excel 问题:当我尝试在新创建的工作簿中隐藏特定的多行范围时,出现运行时 450 错误 - VBA/Excel Issue: When I tried to hide specific multiple row ranges in a newly created workbook, i get a runtime 450 error 将工作表移动到新创建的和版本更改的工作簿VBA - Moving Worksheets to Newly Created & Version Changed Workbook VBA 直接将新创建的Excel工作簿添加到Outlook - Add Newly Created Excel Workbook onto Outlook Directly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM