請問各位大大!
目前我有在"C:\維修單\"中有不同縣市(共12個)的資料夾..
且在"C:\維修單\高雄市\10月份\"底下有數量不一的維修單EXCEL檔案..
檔案名稱是:108年10月19日維修單(廠商名字).xlsx
而且是以當天的日期做檔案名稱標準..
我想把不同縣市的所有維修檔案中特定的幾個儲存格
如:"C1:D1","L1","I8:K8"等的內容以新開檔案(維修總表)的方式儲存到sheet(1):10月份中..
不知道該怎麼寫該程式..
請教各位大大一下~!
謝謝~!
流程:
先用變數記錄當前的的Excel (W1)
指定W1要填值的儲存格 (R1)
用迴圈進入各個子資料夾去抓各個文件
用Workbooks.Open開啟Excel (W2)
將W2特定儲存格的值指派給R1
填值完成後,讓R1指定的儲存格往下位移
關閉W2
開啟迴圈下一個Excel重複上述動作
持續上述動作到路徑下所有資料夾內的所有文件都做過一輪後結束
注意:
若Excel事先開啟,Workbooks.Open時會出錯
Sub Test()
Dr1 = "C:\維修單"
Set W1 = ActiveWorkbook
Set S1 = W1.Sheets(1)
Set R1 = S1.[A1]
With CreateObject("Scripting.FileSystemObject")
Set Drs = .GetFolder(Dr1).SubFolders
For Each Dr In Drs
Set Files = .GetFolder(Dr).Files
For Each File In Files
Workbooks.Open (File.Path)
Set W2 = Workbooks(File.Name)
Set S2 = W2.Sheets(1)
Range(R1, R1.Offset(0, 1)).Value = S2.[C1:D1].Value
R1.Offset(0, 2).Value = S2.[L1].Value
Range(R1.Offset(0, 3), R1.Offset(0, 5)).Value = S2.[I8:K8].Value
W2.Close
Set R1 = R1.Offset(1, 0)
Next File
Next Dr
End With
End Sub
詳細請參考
https://support.microsoft.com/en-us/help/291295/macro-code-to-check-whether-a-file-is-already-open
Sub TestFileOpened()
' Test to see if the file is open.
If IsFileOpen("c:\Book2.xls") Then
' Display a message stating the file in use.
MsgBox "File already in use!"
'
' Add code here to handle case where file is open by another
' user.
'
Else
' Display a message stating the file is not in use.
MsgBox "File not in use!"
' Open the file in Microsoft Excel.
Workbooks.Open "c:\Book2.xls"
'
' Add code here to handle case where file is NOT open by another
' user.
'
End If
End Sub
' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error occurs because there is
' some other problem accessing the file.
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
f10629 wrote:
以下範例給您參考流程...(恕刪)
請問大大!!
因此程式是針對目標檔案中特定格式做一次性的抓取動作!
但某些資料夾中有些檔案內有多筆資料要抓取的話要如何處理呢?
也就是如:L1欄位有一次性的資料..
但D8:E8及I8:K8欄位有多筆資料(同一個檔案中)
那該如何處理呢?
謝謝大大!!
Sub 維修單單一分店資料匯入總表()
Dim W1, W2, S1, S2, R1, Dr1 As String
Dim Dr, File, files, Drs As Variant
Dim i As Integer
Application.ScreenUpdating = False '為了避免螢幕更新,造成測試時間拉長,先關閉
Application.DisplayAlerts = False '在巨集執行時隱藏提示和警告訊息不顯示出來
Dr1 = "C:\新版維修單\博愛\2019年"
Set W1 = ActiveWorkbook
Set S1 = W1.Sheets(2) '把抓取下來的資料放在分頁2中
Set R1 = S1.Range("A2") 'Set R1 = ActiveWorkbook.Sheets(1).[A1]
With CreateObject("Scripting.FileSystemObject")
Set Drs = .GetFolder(Dr1).SubFolders
For Each Dr In Drs '變數Dr在資料夾裡面做搜尋動作
Set files = .GetFolder(Dr).files
For Each File In files
Workbooks.Open (File.Path)
Set W2 = Workbooks(File.Name)
Set S2 = W2.Sheets(1)
Range(R1, R1.Offset(0, 1)).Value = S2.[C1:D1].Value '抓取C1D1的資料出來
R1.Offset(0, 2).Value = S2.Range("L1").Value '抓取L1的資料出來
'以下為測試單一檔案中有多筆資料要抓取
For i = 1 To 15
Range(R1.Offset(0, 3 + i), R1.Offset(0, 5 + i)).Value = S2.[D7+i:E7+i].Value '抓取D8E8的資料出來
Next i
'以上為測試單一檔案中有多筆資料要抓取
W2.Close
Set R1 = R1.Offset(1, 0)
Next File
Next Dr
End With
Application.ScreenUpdating = True
'Application.DisplayAlerts = True
End Sub
內文搜尋

X