[英]Combine multiple workbooks each with one sheet into one workbook with multiple sheets
我是 VBA 的新手,我正在嘗試執行以下操作:
我想編寫一個 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.