遇到問題:
檔案『list20180206135132.xls』除了 list 為固定字串,後面的日期時間數字均不固定,包含檔案內的活頁名稱也是一樣只有 list 為固定字串,後面的日期時間數字均不固定
因此巨集在寫的時候希望可以改為開啟指定關鍵字檔名『list』的檔案,例如list201802081532、list201803290915…以此類推,只要檔名有『list』關鍵字即符合條件,活頁名稱也是一樣
Option Explicit
Sub Ex()
Dim xDir1 As String, xDir2 As String, xDir3 As String, xPath As String, xWb1 As Workbook, xWb2 As Workbook, xWb3 As Workbook
Dim Sh1(), Sh2(), Dir_Ar1(), Dir_Ar2(), xRng1(), xRng2(), i As Integer
Dir_Ar1 = Array("list*.xls", "CCMOPQ*.xls", "CCMOP_NAME*.xls")
Dir_Ar2 = Array("CCMOPQ*.xls", "CCMOP_NAME*.xls")
Sh1 = Array("list報表", "有資料1", "無資料1")
Sh2 = Array("有資料2", "無資料2")
xRng1 = Array("A1", "B1", "B1")
xRng2 = Array("B1", "B1")
xPath = ThisWorkbook.Path
For i = 0 To UBound(Sh1)
xDir1 = Dir(xPath & "\" & Dir_Ar1(i), vbDirectory)
Do While xDir1 <> ""
If i = UCase(xDir1) Then GoTo xNext1
Set xWb1 = Workbooks.Open(xPath & "\" & xDir1)
With ThisWorkbook.Sheets(Sh1(i)).Range(xRng1(i)).End(xlDown)
If .Row = .Parent.Rows.Count Then
xWb1.Sheets(1).UsedRange.Copy .Cells.End(xlUp)
Else
xWb1.Sheets(1).UsedRange.Copy .Cells.Offset(1)
End If
End With
xWb1.Close
xNext1:
xDir1 = Dir
Loop
Next
For i = 0 To UBound(Sh2)
xDir2 = Dir(xPath & "\" & Dir_Ar2(i), vbDirectory)
Do While xDir2 <> ""
If i = UCase(xDir2) Then GoTo xNext2
Set xWb2 = Workbooks.Open(xPath & "\" & xDir2)
With ThisWorkbook.Sheets(Sh2(i)).Range(xRng2(i)).End(xlDown)
If .Row = .Parent.Rows.Count Then
xWb2.Sheets(2).UsedRange.Copy .Cells.End(xlUp)
Else
xWb2.Sheets(2).UsedRange.Copy .Cells.Offset(1)
End If
End With
xWb2.Close
xNext2:
xDir2 = Dir
Loop
Next
xDir3 = Dir(xPath & "\預約表單*.xls", vbDirectory)
Do While xDir3 <> ""
Set xWb3 = Workbooks.Open(xPath & "\" & xDir3)
With ThisWorkbook.Sheets("預約表單").Range("A1").End(xlDown)
If .Row = .Parent.Rows.Count Then
xWb3.Sheets("預約表單系統資料").UsedRange.Copy .Cells.End(xlUp)
Else
xWb3.Sheets("預約表單系統資料").UsedRange.Copy .Cells.Offset(1)
End If
End With
xWb3.Close
xDir3 = Dir
Loop
End Sub作者: Hsieh 時間: 2018-3-5 16:11