Option Explicit
Sub Ex()
Dim Rng As Range, D As Object, E As Range
With Sheets("Form")
Set Rng = .Range(.[d2], .Range("d" & .[a2].End(xlDown).Row)) '符合運動時間的列位
End With
Rng = "=IF(rc[-1]-rc[-2]>=TIMEVALUE(""02:00:00""),""符合"",""不符合"")" '寫上公式 (健身房離開時間-健身房進入時間>=02:00:00)
Rng.Value = Rng.Value '公式轉為值
Set D = CreateObject("Scripting.Dictionary") '字典物件
For Each E In Rng 'Rng.Cells 的迴圈
D(E.Offset(, -3).Text) = E.Text '關鍵字-> E.Offset(, -3)為姓名帶入 (符合/不符合)
Next
'重設Rng變數
Set Rng = Workbooks.Open("\\Test\B.xlsx").Sheets("Time").Range("A:A").SpecialCells(xlCellTypeConstants)
For Each E In Rng
If D.EXISTS(E.Text) Then E.Offset(, 1) = D(E.Text) 'E為A欄 -> E.Offset(, 1)= B欄
'字典物件的Exists 方法 :字典物件的關鍵字存在時為TTrue, 執行(Then .... )
Next
Rng.Parent.Parent.Close False
'Rng.Parent 是Sheets("Time")
'Rng.Parent.Parent為Workbook
End Sub
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/) |