- 帖子
- 8
- 主題
- 4
- 精華
- 0
- 積分
- 55
- 點名
- 0
- 作業系統
- Windows 10
- 軟體版本
- Office 2013
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2013-11-29
- 最後登錄
- 2021-11-17
|
抓取來源資料另存在目的不同sheet中
各位先進們好
小弟有幾個來源文件(檔名相同sheet內容不同),還有一個目的文件分成好幾個sheet(各sheet中各有對應來源檔),並指定輸入要的時間匯入
有個小問題
1.讀取來源檔以陣列讀取完後往下一列繼續不是往下一欄繼續,如果我的陣列類型是欄列都有的話。
2.讀取完要寫入目的檔時會寫入全部sheet(會在A1寫入日期B2寫入時間其餘空白) 而不是只寫入其中一個sheet內
請問各位先進該如何更改- '每日報表匯入總日報表'
- Sub Test()
- Dim sFile, wbThis As Workbook, wbSrc As Workbook
- Dim lRowIndex As Long, lColIndex As Long, oDic As Object, aTime, sDate
- Dim sType As String, lShIndex As Long, lTimeIndex As Long
- Const lBlkWidth As Long = 13
- Const lBlkHeight As Long = 40
- sFile = Application.GetOpenFilename(fileFilter:="Excel Files (*.xls),*.xls", Title:="選擇日報表")
- If StrComp(TypeName(sFile), "Boolean", vbTextCompare) = 0 Then Exit Sub
- aTime = InputBox("輸入要記錄的時間" & "以逗號分開,不要空格)", , "00:00,08:00,16:00")
- If aTime = "" Then Exit Sub
- aTime = Split(aTime, ",")
- Application.ScreenUpdating = False
- Set wbThis = ThisWorkbook
- Set wbSrc = Workbooks.Open(Filename:=sFile, ReadOnly:=True)
- Set oDic = CreateObject("Scripting.Dictionary")
- With wbSrc.Sheets(1)
- sDate = .Range("TitleDate").Value
- For lRowIndex = .[A8].Row To .UsedRange.Rows.Count Step lBlkHeight
- For lColIndex = 2 To lBlkWidth
- sType = .Cells(lRowIndex, lColIndex).Value
- If sType <> "" And Left(sType, 1) <> "@" Then
- If Not oDic.exists(sType) Then
- Set oDic(sType) = CreateObject("scripting.dictionary")
- For Each x In .Cells(lRowIndex, 1).Offset(4).Resize(24)
- If x.Value <> "" Then oDic(sType)(x.Text) = x.Offset(, lColIndex - 1).Value
- Next
- End If
- End If
- Next
- Next
- End With
- wbSrc.Close
- Application.ScreenUpdating = True
- With wbThis
- For lShIndex = 1 To .Sheets.Count
- With .Sheets(lShIndex)
- lRowIndex = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
- .Cells(lRowIndex, "A").Value = sDate
- .Cells(lRowIndex, "B").Resize(UBound(aTime) + 1) = Application.Transpose(aTime)
- For lColIndex = 3 To .[C4].End(xlToRight).Column
- If oDic.exists(.Cells(4, lColIndex).Value) Then
- For lTimeIndex = 0 To UBound(aTime)
- .Cells(lRowIndex, lColIndex).Offset(lTimeIndex).Value = oDic(.Cells(4, lColIndex).Value)(aTime(lTimeIndex))
- Next
- oDic(.Cells(4, lColIndex).Value).RemoveAll
- oDic.Remove .Cells(4, lColIndex).Value
- End If
- Next
- End With
- Next
- End With
- MsgBox "目標檔找不到欄位資料: " & vbCrLf & Join(oDic.keys, ", ")
- 'cleanup
- For Each x In oDic
- oDic(x).RemoveAll
- oDic.RemoveAll
- Next
- End Sub
複製代碼 |
-
-
Test.rar
(482.61 KB)
|