VBA 复制工作表

Worksheet.Copy 复制工作表。


语法

Copy Before , After

复制到指定位置。

如果没有指定位置,则新建工作簿,然后复制到新建工作簿中;

指定位置,可以是同一个工作簿,或其它打开的工作簿。


示例

例1:复制到新建工作簿。

Sub sub9_1()
  Dim wb As Workbook
  Dim ws As Worksheet

  Set wb = Workbooks("小步教程1.xlsx")
  Set ws = wb.Worksheets("Sheet1")

  'Copy无参表示先新建工作簿,再复制
  wb.Worksheets("Sheet1").Copy

End Sub

例2:复制到同一工作簿。

Sub sub9_2()

  Dim wb As Workbook
  Dim ws As Worksheet

  Set wb = Workbooks("小步教程1.xlsx")
  Set ws = wb.Worksheets("Sheet1")

  '复制工作表到最后位置
  wb.Worksheets("Sheet1").Copy after:=wb.Worksheets.Item(wb.Worksheets.Count)
  '修改名称
  wb.Worksheets.Item(wb.Worksheets.Count).Name = "Sheet1-1"

End Sub

例3:复制到其它工作簿。从 小步教程1.xlsx 复到到 小步教程.xlsx

Sub sub9_3()

  Dim wb, wb2 As Workbook
  Dim ws As Worksheet

  Set wb = Workbooks("小步教程1.xlsx")
  Set wb2 = Workbooks("小步教程2.xlsx")

  Set ws = wb.Worksheets("Sheet1")

  '复制到另一工作簿
  ws.Copy after:=wb2.Worksheets.Item(wb.Worksheets.Count)

End Sub