简体   繁体   English

如何在Excel中使用其目录路径而不是使用vba进行代码编码来关闭打开的工作簿?

[英]How can I code to close an open workbook using its directory path instead of its name using vba in excel?

So I've written a script that opens a certain workbook using its directory pathway (through text from a userform textbox) and I want to be able to close it at the end of the script. 因此,我编写了一个脚本,该脚本使用其目录路径(通过用户窗体文本框中的文本)打开某个工作簿,并且希望能够在脚本末尾将其关闭。 My script currently opens a workbook using the file directory and copies something from that workbook and pastes it into the current workbook. 我的脚本当前使用文件目录打开一个工作簿,并从该工作簿中复制某些内容并将其粘贴到当前工作簿中。 The only thing I want to add is that workbook closing at the end of the sub. 我唯一要添加的是工作簿在子结尾处关闭。

Sub A()

    Dim wbk As Workbook
    strFirstFile = Userform1.Command.Text
    Set wbk = Workbooks.Open(strFirstFile)
    With wbk.Sheets("Sheet1")
    Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.Copy
    End With
    Set wbk = ThisWorkbook
    wbk.Sheets("dog").Range("A1").Insert

End Sub

Bear with me I'm a super newbie. 忍受我,我是超级新手。

To close the Workbook: 要关闭工作簿:

wbk.Close

If you want to save the workbook beforehand, do: 如果要预先保存工作簿,请执行以下操作:

wbk.Save
wbk.Close

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

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