excel-vba 始终定义和设置对所有工作簿和工作表的引用
示例
当使用多个打开的工作簿(每个工作簿可能有多个工作表)时,定义和设置对所有工作簿和工作表的引用是最安全的。
不要依赖于ActiveWorkbook或ActiveSheet因为它们可能被用户改变。
下面的代码示例演示如何将范围从“复制Raw_Data ”片材在“ Data.xlsx ”工作簿“ Refined_Data在”“片Results.xlsx ”工作簿。
该过程还演示了如何在不使用该Select方法的情况下进行复制和粘贴。
Option ExplicitSub CopyRanges_BetweenShts()
Dim wbSrc As Workbook
Dim wbDest As Workbook
Dim shtCopy As Worksheet
Dim shtPaste As Worksheet
' set reference to all workbooks by name, don't rely on ActiveWorkbook
Set wbSrc = Workbooks("Data.xlsx")
Set wbDest = Workbooks("Results.xlsx")
' set reference to all sheets by name, don't rely on ActiveSheet
Set shtCopy = wbSrc.Sheet1 '// "Raw_Data" sheet
Set shtPaste = wbDest.Sheet2 '// "Refined_Data") sheet
' copy range from "Data" 工作簿 "Results" workbook without using Select
shtCopy.Range("A1:C10").Copy _
Destination:=shtPaste.Range("A1")
End Sub
以上是 excel-vba 始终定义和设置对所有工作簿和工作表的引用 的全部内容, 来源链接: utcz.com/z/326251.html