0

我有两个单独的工作簿,一个名为 ActiveWorkbook,另一个名为 Terminated Workbook。我想要做的是在 ActiveWorkbook--Templatesheet 上创建一个复制按钮,它将询问用户将哪个工作表复制并复制到 TerminatedWokbook 并命名为与原始工作表相同。

我有一些这样的代码,因为我对 excel 宏很陌生,所以它不起作用。谢谢

Sub CopytoTernimal()
Dim CopyName As String
CopyName = InputBox("Please enter the name of sheet which will copy to ternimal")

Sheets("CopyName").Copy
Before:=Workbooks("Terminated Employees").Sheets(1)

End Sub
4

1 回答 1

2

好的,这是完整的代码

Dim CopyName As String
CopyName = InputBox("Please enter the name of sheet which will copy to ternimal")

Dim thisSheet As Worksheet

'you must be sure CopyName is typed correctly, or it wont find the sheet
'also be sure the Activeworkbook name is correctly typed. 
Set thisSheet = Workbooks("ActiveWorkbook").Worksheets(CopyName) 

'copy this sheet
thisSheet.Rows.Copy

Dim NewSheet As Worksheet
Set NewSheet = Workbooks("Terminated Employees").Worksheets.Add()
NewSheet.Name = thisSheet.Name
NewSheet.Paste

要使其成为按钮,请转到主 Excel 窗口的开发人员选项卡,然后插入一个Active X Button. (使用活动工作簿)然后在 中design mode,双击该按钮。将click event自动生成,因此您将此代码放在该子目录中。之后,禁用design mode(也在主窗口的开发人员选项卡中)。单击按钮时,将调用代码。

于 2013-07-23T15:57:24.857 回答