我有两个单独的工作簿,一个名为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
答案 0 :(得分: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
(也在主窗口的开发人员选项卡中)。
单击该按钮时,将调用代码。