我是 VBA 新手,我正在嘗試執行以下操作:
- 我有多個 CSV 檔案,每個檔案都有一個作業表,作業簿以特定名稱保存。
- 所有作業簿的格式都相同。
我想撰寫一個 VBA 代碼,它將在名為 RDI raw data.xlsm 的單獨作業簿中執行以下操作
在 RDI 原始資料中
將作業簿中的所有資料復制到 RDI 原始資料檔案中,但每個作業簿都需要是 RDI 原始資料檔案中的單獨作業表
有人可以幫忙嗎?
uj5u.com熱心網友回復:
將 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/508293.html
上一篇:Visio資料記錄集物件
下一篇:加速和簡化