- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
6#
發表於 2013-12-5 15:10
| 只看該作者
回復 5# rockrun
試試看- 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 = 36
-
- sFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls),*.xls", Title:="選擇檔案")
- If StrComp(TypeName(sFile), "Boolean", vbTextCompare) = 0 Then Exit Sub
-
- aTime = InputBox("輸入要記錄的時間" & "(以逗號分開,不要空格)", , "00:00,04:00,08: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 = .[A2].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.Remove
- Next
複製代碼 |
|