- 帖子
- 218
- 主題
- 73
- 精華
- 0
- 積分
- 290
- 點名
- 0
- 作業系統
- WIN10
- 軟體版本
- Office2010
- 閱讀權限
- 20
- 性別
- 女
- 註冊時間
- 2014-5-19
- 最後登錄
- 2022-11-29
|
10#
發表於 2018-3-1 22:58
| 只看該作者
回復 8# GBKEE
G大~~~~我自己大概試改了一下,可以請你幫我看看醬對不對嗎?但我已經盡力了............ 然後CCMOPQ*.xls我最後多加了一下Q,因為"CCMOPQ*.xls", "CCMOP_NAME*.xls"這兩個檔名前面不會完全一樣,抱歉
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 |
|