简体   繁体   English

Excel VBA代码-如何对创建的文件调用某些操作

[英]Excel VBA Code - How to call certain actions to files created

I am writing a piece of code in Excel VBA in which I needed to create a macro which allows the user to click the ActiveX button as a result of which the file is then saved to a specified location. 我正在Excel VBA中编写一段代码,其中需要创建一个宏,该宏允许用户单击ActiveX按钮,然后将文件保存到指定位置。 Once this new file is created, I wanted to code so the new file (which successfully saves in the alternate specified location) does not have the ActiveX Command Button is not present. 创建此新文件后,我要进行编码,以便新文件(已成功保存在备用指定位置)没有ActiveX命令按钮。 Also, once the button is clicked from the original file, I wanted to somehow make the master file close and the newly saved file to automatically open. 另外,一旦从原始文件中单击按钮,我就想以某种方式关闭主文件,并使新保存的文件自动打开。 Please can someone help? 请有人帮忙吗?

Code so far: 到目前为止的代码:

Sub CommandButton1_Click()

    ActiveSheet.Copy
    Dim SaveName As String
    SaveName = ActiveSheet.Range("C1").Text
    With ActiveWorkbook
    .SaveAs "File path Specified" & _
    SaveName & ".xls"
    .Close 0
    End With

End Sub

My first solution (depending on what you really need to do) is the following: 我的第一个解决方案(取决于您的实际需求)如下:

Firstly you will need this: 首先,您将需要以下内容:

Me.SaveCopyAs "<full_Path>"

See more on this here: https://msdn.microsoft.com/en-us/library/office/ff835014.aspx 在此处查看更多信息: https : //msdn.microsoft.com/en-us/library/office/ff835014.aspx

This will create a copy of the file to the specified path with whatever name you want. 这将使用您想要的任何名称将文件的副本创建到指定路径。 Before you do that, you could hide your button and then use save as copy to save it with the button hidden. 在执行此操作之前,您可以隐藏按钮,然后使用“另存为副本”在按钮隐藏的情况下进行保存。

Finally if you want to close the original and open the copy then you have to give to the copy a different name. 最后,如果您要关闭原件并打开副本,则必须给副本一个不同的名称。 Then open the new file and close the original. 然后打开新文件并关闭原始文件。

Your code should look similar to this: 您的代码应类似于以下内容:

Sub CommandButton1_Click()
    ActiveSheet.Copy
    Dim SaveName As String
    SaveName = ActiveSheet.Range("C1").Text
    With ActiveWorkbook
        .Worksheets("<your_worksheet>").CommandButton1.visible = False
        .SaveCopyAs "File path Specified" & SaveName & ".xls"
    End With
    Workbooks.Open ("File path Specified" & SaveName & ".xls")
    Workbooks("<Original_name.xlsm>").close False
End Sub

Another Solution could be saving the workbook with SaveAs. 另一个解决方案可能是使用SaveAs保存工作簿。 Before that save the orginal. 在此之前保存原始。 Hide the button. 隐藏按钮。 And saveas will close the original and open the new one automatically. saveas将关闭原始文件并自动打开新文件。

Your code should look something like that: 您的代码应如下所示:

Sub CommandButton1_Click()
    ActiveSheet.Copy
    Dim SaveName As String
    SaveName = ActiveSheet.Range("C1").Text
    With ActiveWorkbook
        .Save
        .Worksheets("<your_worksheet>").CommandButton1.visible = False
        .SaveAs "File path Specified" & SaveName & ".xls"
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM