【问题标题】:How to migrate many excel sheets to one master workbook如何将多个 Excel 工作表迁移到一个主工作簿
【发布时间】:2021-08-17 05:41:55
【问题描述】:

我通常有几个带有单独工作表的工作簿要迁移到主工作簿,想想:

  • Jan.xlsx
  • 二月.xlsx
  • Mar.xlxs
  • ...
  • Dec.xlsx

在名为 2020Recap.xlxm 的单张纸上进行回顾。我想构建一个宏来收集每个工作表并写入回顾工作簿,我从下面的 VBA 脚本开始。

我在使用下面的 VBA 时遇到问题

我还想添加到宏中以使用文件名作为工作表名称。

Sub CopySheets()

Workbooks("C:\Test\Account AR Aging Patient.xlsx").Sheets("Account AR Aging Patient.xlsx").Copy _
  After:=Workbooks("c:\Test\MEBIllingOffice.xlsm").Sheets(Workbooks("c:\Test\MEBIllingOffice.xlsm").Sheets.Count)

Workbooks("C:\Test\Account AR Aging Payer.xlsx").Sheets("Account AR Aging Payer.xlsx").Copy _
    After:=Workbooks("c:\Test\MEBIllingOffice.xlsm").Sheets(Workbooks("c:\Test\MEBIllingOffice.xlsm").Sheets.Count)

Workbooks("C:\Test\AR History.xlsx").Sheets("AR History").Copy _
    After:=Workbooks("c:\Test\MEBIllingOffice.xlsm").Sheets(Workbooks("c:\Test\MEBIllingOffice.xlsm").Sheets.Count)

End Sub


【问题讨论】:

  • 看看这个:stackoverflow.com/q/30575923/4961700,如果你觉得有用就投票给它。
  • 有什么问题?
  • 我收到运行时错误 9 下标超出范围消息
  • 什么时候出现错误?哪一行?
  • 在第一行,它似乎是文件和目录名称

标签: excel vba


【解决方案1】:

您在工作表名称中有工作簿名称。试试:

Dim wb_source As Workbook
Dim wb_target As Workbook

Application.DisplayAlerts = False

Set wb_source = Workbooks.Open("C:\Test\Account AR Aging Patient.xlsx")
Set wb_target = Workbooks.Open("c:\Test\MEBIllingOffice.xlsm")

'assuming the sheet name is "Sheet1"
wb_source.Sheets("Sheet1").Copy after:=wb_target.Sheets(wb_target.Sheets.Count)
wb_source.Close

'next source.  note: sheet names must be unique within target
Set wb_source = Workbooks.Open("C:\Test\Account AR Aging Payer.xlsx")
wb_source.Sheets("Sheet2").Copy after:=wb_target.Sheets(wb_target.Sheets.Count)
wb_source.Close

wb_target.Save
wb_target.Close

Application.DisplayAlerts = True

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2011-04-18
    • 2021-01-24
    相关资源
    最近更新 更多