標題:
[發問]
請問來源資料自動判定時間輸入相應的sheet內
[打印本頁]
作者:
rockrun
時間:
2013-12-4 22:48
標題:
請問來源資料自動判定時間輸入相應的sheet內
各位前輩及大大們好
小弟有個問題
有兩個來源檔資料要匯入相應的工作表中
[attach]16965[/attach]
[attach]16966[/attach]
來源檔的中的1、2和B2~M2、B38~M38、B74~M74.....抬頭名稱和目的檔3的B4~BT4抬頭名稱相同但"工作表"不同
目的檔的時間點要抓取來源檔的N2後匯入需要的時間點,由於來源檔每次檔名會不同但格式相同
來源檔匯入目的檔所需資料後下次資料匯入會從下一列以同樣的方法作業
[attach]16964[/attach]
[attach]16963[/attach]
作者:
stillfish00
時間:
2013-12-4 23:24
本帖最後由 stillfish00 於 2013-12-4 23:26 編輯
回復
1#
rockrun
抬頭名稱和目的檔不同呢,像B38 或 B74 ....
作者:
rockrun
時間:
2013-12-4 23:31
本帖最後由 rockrun 於 2013-12-4 23:33 編輯
回復
2#
stillfish00
抱歉抱歉 小弟眼挫 每月keyin那麼久卻沒發現 :dizzy:
[attach]16967[/attach]
作者:
stillfish00
時間:
2013-12-5 07:58
回復
3#
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
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
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
aTime = .[A6].Resize(24).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
oDic(sType) = .Cells(lRowIndex, lColIndex).Offset(4).Resize(24).Value
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(24) = aTime
For lColIndex = 3 To .[C4].End(xlToRight).Column
If oDic.exists(.Cells(4, lColIndex).Value) Then
.Cells(lRowIndex, lColIndex).Resize(24).Value = oDic(.Cells(4, lColIndex).Value)
oDic.Remove .Cells(4, lColIndex).Value
End If
Next
End With
Next
End With
MsgBox "目標檔找不到欄位的資料: " & vbCrLf & Join(oDic.keys, ", ")
End Sub
複製代碼
作者:
rockrun
時間:
2013-12-5 12:03
回復
4#
stillfish00
非常感謝 stillfish00 大大的幫忙
如果要取固定時間值的話該怎麼做(如 00:00、04:00、08:00、......之類)
作者:
stillfish00
時間:
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
複製代碼
作者:
rockrun
時間:
2013-12-5 19:28
回復
6#
stillfish00
感謝 stillfish00 大大的幫忙
大致問題都解決了,非常感謝:'(
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)