简体   繁体   English

VBA我如何打开另一个工作簿?

[英]VBA how do i open another workbook?

I am trying to open another workbook, using the code below 我正在尝试使用下面的代码打开另一个工作簿

Sheets("Range").Activate
   Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Workbooks.Open ("AvgStdev.xlsm")

And it was working before, but now excel is prompting the file cant be found. 它以前工作,但现在excel促使文件无法找到。 Help please :/ 请帮助 :/

You can do what you want easily if you declare your variable as discussed HERE . 如果您按照此处的说明声明变量,则可以轻松地执行您想要的操作。
So if we are to apply it, you can open your workbook like this: 因此,如果我们要应用它,您可以像这样打开工作簿:

    Dim wb As Workbook
    Dim myfilename As String

    myfilename = "C:\Users\Ayaz\Desktop\Analysis\AvgStdev.xlsm"
    '~~> open the workbook and pass it to workbook object variable
    Set wb = Workbooks.Open(myfilename) 

    '~~> More codes here

Now later in your code if you are saving the same file: 如果要保存相同的文件,现在稍后在代码中:

    wb.Save '~~> save
    wb.Close '~~> close

Or you can do it using Close method only: 或者你可以只使用Close方法:

    wb.Close True '~~> explicit SaveChanges argument to true

Now if however you like to save it as another file: 现在,如果您想将其另存为另一个文件:

    Dim newfilename As String
    newfilename = "C:\Users\Ayaz\Desktop\Analysis\Another.xlsm"
    '~~> If you are saving it in a format other than .xlsx,
    '~~> you have to be explicit in the FileFormat argument
    wb.SaveAs newfilename, xlOpenXMLWorkbookMacroEnabled
    wb.Close

HTH. HTH。

右键单击excel文件并从属性中获取文件路径,然后将实际文件路径替换为以下内容:

Workbooks.Open ("filepath\AvgStdev.xlsm")

暂无
暂无

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

相关问题 如何在VBA中打开现有工作簿? - How do I open an existing workbook in VBA? 如何使用 VBA 中的输入框从已打开的 Excel 工作簿中获取用户的范围并将其粘贴到另一个标准化工作簿中? - How do I use the Inputbox in VBA to obtain a range from a user from a already open Excel workbook and paste it into another, standardized workbook? 如何在VBA中打开并激活另一个工作簿? - How to open and activate another workbook in VBA? 如何更快地打开此VBA工作簿? - How can I open this VBA workbook faster? 如何使用VBA打开宏工作簿? - How can I open a macro workbook with VBA? 我可以在单元格公式内的另一个打开的工作簿中使用另一个打开的工作簿的 VBA 函数吗? - Can I use another open workbook's VBA Functions in another open workbook within a Cell formula? 当宏在另一个工作簿中运行时,如何打开工作簿? - How do you open a workbook whilst a macro is running in another workbook? 在 Excel 中使用 VBA 为目录名称执行 Workbook.open 时如何使用通配符? - How do I use a wildcard when using VBA in Excel to do Workbook.open for a directory name? 如何在vba中添加库引用,以便每次打开新工作簿时都会添加它? - How do I add a library reference in vba so that it remains added every time I open a new workbook? Excel VBA - 如何清除另一个应用程序中另一个工作簿上的剪贴板? - Excel VBA - How do I clear the clipboard on another workbook in another application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM