[英]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.