将一个选项卡从工作表复制到另一个工作簿

时间:2014-06-04 11:41:35

标签: excel excel-vba vba

我正在编写宏来将活动工作簿的选项卡选项卡复制到不同的工作簿。基本上我想做的是:

  • 按按钮
  • 复制标签名为" 1。"
  • 打开名为" 1.xlsx"
  • 的其他工作簿
  • 将标签粘贴为最后一个
  • 重命名

我尝试使用以下代码执行此操作:

Sub Copy()

'Open the sheet in which I want to copy the tab

Workbooks.Open ("C:\Users\aa471714\Desktop\RTS\1.xlsx")

'Copy the sheet from an activeworkbook to the tab after the excelsheet i just opened (1.xlsx)

ActiveWorkbook.Sheets("3.").Copy _
after:=Workbooks("1.xlsx").Sheets("1.")

'Define new sheetname
shtname = InputBox("What's the new sheet name?", "Sheet name?")
ActiveSheet.Name = "3_" & shtname

End Sub

但是我得到了一个错误9.有关快速修复的想法吗?

此致 马克

1 个答案:

答案 0 :(得分:0)

ActiveWorkbook

可能存在问题

这有帮助吗?

Sub Copy()
    Dim source As Workbook, sht As Worksheet
    Set source = ActiveWorkbook
    Set sht = source.Sheets("3.")

'Open the sheet in which I want to copy the tab

    Workbooks.Open ("C:\Users\aa471714\Desktop\RTS\1.xlsx")

'Copy the sheet from original to the tab after the excelsheet i just opened (1.xlsx)

    sht.Copy after:=ActiveWorkbook.Sheets("1.")

'Define new sheetname

    shtname = InputBox("What's the new sheet name?", "Sheet name?")
    ActiveSheet.Name = "3_" & shtname

End Sub