返回列表 上一主題 發帖

[發問] 將資料寫入到其他多個EXCEL檔案

回復 20# mark761222


    在匯出資料在中的工作表名稱和目的的工作表名稱有誤,可能是因為空格的關係,請自行修正否則會有 aaa.png 的錯誤產生
  1. Sub 按鈕1_Click()
  2.     Application.ScreenUpdating = False
  3.     Dim d As Object
  4.     Dim xlPath As Variant, xlFile As Variant, aa As Variant
  5.     Dim Rng As Range, Rn As Range, Ran As Range, ch As Range
  6.     Dim myRow As Integer, myCol As Integer, k As Integer, I As Integer, j As Integer, xlRow As Integer
  7.     xlPath = ThisWorkbook.Path & "\"
  8.     Set d = CreateObject("scripting.dictionary") '設定d為字典物件
  9.     With Sheets("匯出資料")
  10.         For Each Rng In .Range("A3", Cells(Rows.Count, 1).End(xlUp)) '此迴圈是讀取檔案名稱
  11.             If Rng <> "" Then
  12.                 d(Rng.Value) = ""
  13.             End If
  14.         Next
  15.         myRow = .Cells(Rows.Count, 1).End(xlUp).Row '查詢"匯出資料"的最後一列位置
  16.         For Each Rng In .Range("B2", .Cells(myRow, 2)) '此迴圈做 有資料Range位置的聯集 Union,讀取工作表名稱
  17.             If Rng <> "" Then
  18.                 k = k + 1
  19.                 If k = 1 Then
  20.                     Set Rn = Rng
  21.                 Else
  22.                     Set Rn = Union(Rn, Rng)
  23.                 End If
  24.             End If
  25.         Next
  26.     End With
  27.         xlFile = d.keys '將字典的key值給予xlFile(為陣列),以目前讀取的檔案名稱有"Daily Yield Rate report 分廠 2015BR"
  28.                         '以及"Daily Yield Rate report(EN)2015BR4"2個檔案
  29.         
  30.     For I = 0 To UBound(xlFile) '以檔案為做為迴圈,來開啟檔案
  31.         With Workbooks.Open(xlPath & xlFile(I) & ".xlsx") '開啟檔案
  32.             For Each Ran In Rn '執行工作表迴圈
  33.                 If Ran.Offset(, -1) Like xlFile(I) Then '比對此工作表是否屬於xlFile(I)檔案,如果是則執行If中程序
  34.                     With .Sheets(Ran.Value)
  35.                         Set ch = .Columns(1).Find(Ran.Offset(, 1), LookAt:=xlWhole, SearchDirection:=2)
  36.                         '檢查日期是否有重複,當ch變數為Nothing時,則無發現重複日期,否則離開這一次的資料儲存,並執行下一個迴圈
  37.                         If Not ch Is Nothing Then MsgBox Ran & "工作表中的" & ch & "資料已存在,不會儲存資料": Set ch = Nothing: GoTo 10
  38.                         myCol = ThisWorkbook.Sheets("匯出資料").Cells(Ran.Row, Columns.Count).End(xlToLeft).Column
  39.                         xlRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1 '讀取目的工作表的最後一列列號
  40.                         For j = 1 To myCol - 2
  41.                             If Ran.Offset(, j).Interior.Color <> 65535 Then '當儲存格色彩不屬於黃色,則執行複製值
  42.                                 .Cells(xlRow, j) = Ran.Offset(, j).Value '複製值
  43.                             End If
  44.                         Next
  45. 10:
  46.                     End With
  47.                 End If
  48.             Next '完成一個工作表後執行下一個工作表
  49.             .Close True '關閉及儲存檔案
  50.         End With
  51.     Next
  52.     Application.ScreenUpdating = False
  53. End Sub
複製代碼

TOP

回復 20# mark761222


    會產生錯誤的工作表名稱為Daily Yield Rate report 分廠 2015B檔案的"B DailyYeild (F1-By Date)"和"T Daily Yeild (F2-By Date )"

TOP

回復 22# lpk187


    謝謝,我已經修正問題,感謝你的幫忙,我會好好研究,增加經驗

TOP

        靜思自在 : 【為善競爭】人生要為善競爭,分秒必爭。
返回列表 上一主題