我正在为 excel 创建一个宏,用于将一个 excel 表复制到另一个 excel 表。
这是有效的:
Function CopyFile()
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set Wb1 = ActiveWorkbook
Set ws1 = Wb1.Worksheets("Task")
Set Wb2 = Workbooks.Open("D:\Outlook\dest.xlsx")
ws1.Copy Wb2.Worksheets(1)
Wb2.Save
Wb2.Close
Wb1.Activate
End Function
但我不想使用硬编码字符串来获取工作表名称 Set ws1 = Wb1.Worksheets("Task")。而是想使用函数参数。
以下代码不起作用:
Function CopyFile(name As String)
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set Wb1 = ActiveWorkbook
Set ws1 = Wb1.Worksheets(name)
Set Wb2 = Workbooks.Open("D:\Outlook\dest.xlsx")
ws1.Copy Wb2.Worksheets(1)
Wb2.Save
Wb2.Close
Wb1.Activate
End Function