繁体   English   中英

将多个工作簿(每个工作簿与一张工作表)合并为一个工作簿与多个工作表

[英]Combine multiple workbooks each with one sheet into one workbook with multiple sheets

我是 VBA 的新手,我正在尝试执行以下操作:

  1. 我有多个 CSV 文件,每个文件都有一个工作表,工作簿以特定名称保存。
  2. 所有工作簿的格式都相同。

我想编写一个 VBA 代码,它将在名为 RDI raw data.xlsm 的单独工作簿中执行以下操作

  • 在 RDI 原始数据中

  • 将工作簿中的所有数据复制到 RDI 原始数据文件中,但每个工作簿都需要是 RDI 原始数据文件中的单独工作表

有人可以帮忙吗?

将 csv 文件放在名为“本地导入”的文件夹中,该文件夹是您保存主 RDI 文件的子文件夹。 该宏将复制 csv 文件中的第一张纸,并放置在您的母版中的第一张纸之后。

Sub cmdImportCSV()

'import multiple sheets in data folder'
Dim wb As Workbook
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim myPath As String
Dim strFilename As String
Dim ws As Worksheet

'skip screen updating and alerts'
Application.ScreenUpdating = False
Application.DisplayAlerts = False


'set path to the folder that contains the worksheets to Import folder'
myPath = ThisWorkbook.Path & "\Import\"

'set import destination to current workbook'
Set wb = ThisWorkbook

'the first file in the source folder'
strFilename = Dir(myPath)

'Speed up importing by not recalculating during loop'
Application.Calculation = xlCalculationManual

'start a loop - import all files in directory'
Do Until strFilename = ""
    'set workbook source'
    Set wbSource = Workbooks.Open(Filename:=myPath & "\" & strFilename)
    'set the worksheet source to copy from'
    Set wsSource = wbSource.Worksheets(1)
    'set where the copy is going to'
    wsSource.Copy after:=wb.Worksheets(1)
    'close the current source workbook'
    wbSource.Close
    'returns the next source workbook'
    strFilename = Dir()
Loop

'Reactivate Automatic calculations'
Application.Calculation = xlCalculationAutomatic

'Reactivate - show screen updated and if errors'
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

如果您想学习 VBA,有几种方法可以实现您的目标,并获得教育。 如果您想要一个不需要编码并获得相同结果的巧妙替代方案,请考虑使用此 Excel 插件。

https://www.rondebruin.nl/win/addins/rdbmerge.htm

它会做你想做的事,它也会做一大堆其他的事情,所有这些都不需要编码,无论如何!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM