作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当在代码中引用不同的工作簿时,我一直在使用“Set Workbooks.Open”。
例子:
`Dim wbname1 as Workbook
Dim wbname2 as Workbook
'code which refers to wbname1
Set wbname1 = Workbooks.Open("path\filename")
'code which refers to wbname2
Set wbname2 = Workbooks.Open("path\filename")`
` 'code which refers to wbname1
Set wbname1 = Workbooks.Open("path\filename")
wbname1.Close True
'code which refers to wbname2
Set wbname2 = Workbooks.Open("path\filename")
'code which refers again to wbname1
Set wbname1 = Workbooks.Open("path\filename")`
Set Workbooks.Open()
最佳答案
Dim book1 as Workbook
Workbook
.当您使用
Set
关键字,您正在为变量分配一个引用。
Set book1 = Workbooks.Open("path\filename1")`
Excel.Application.Workbooks.Open
的结果返回。 ,Excel 对象模型中的一个函数,如果操作成功,它会打开一个工作簿并返回对它打开的工作簿的引用。
This is sometimes inconvenient because in order to refer again to a previous workbook, I would have to close the workbook and reopen it.
Set book1 = Workbooks.Open("path\filename1")
Set book2 = Workbooks.Open("path\filename2")
'work with book1
book1.Sheets(1).Range("A1") = "Hello"
'work with book2
book2.Sheets(1).Range("A1") = "World!"
'all done, clean up now:
book1.Close
book2.Close
关于excel - 在同一代码中使用不同的工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805721/
我是一名优秀的程序员,十分优秀!