用VB需找特定时间
我在文本中有一年的时间数字 如 时间 数字
2013-1-1 8:00:00 ,1
2013-1-2 8:00:00 ,1
2013-1-2 9:00:00 ,1
……………………………………
2013-12-31 7:00:00,1
2013-12-31 8:00:00,1
2013-12-31 23:55:00,1
如这些时间和数字,怎么判断每天有没有8点数字,如果缺失8点钟数字,就在TEXT中显示出来 说某年某月某日 缺八点钟数字。
[解决办法]
Dim strLastDay As String, strToDay As String, strTmp As String, have8Clock As Boolean
Dim strItem() As String
Open "c:\1.txt" For Input As #1
Do Until EOF(1)
Line Input #1, strTmp
strItem = Split(Replace(Replace(strTmp, ":", ":"), ",", ","), ",")
strToDay = Format(CDate(Trim(strItem(0))), "yyyy-mm-dd HH:nn:ss")
If strLastDay > "" And strLastDay <> Left(strToDay, 10) Then
If Not have8Clock Then
Debug.Print strLastDay
End If
have8Clock = False
End If
If Right(strToDay, 8) = "08:00:00" Then
have8Clock = True
End If
strLastDay = Left(strToDay, 10)
Loop
Close #1
If Not have8Clock Then
Debug.Print strLastDay
End If
Option Explicit
Private Declare Function SendMessagebyString Lib _
"user32" Alias "SendMessageA" (ByVal hWND As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As String) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Sub Command1_Click()
Dim strFirstDate, strLastDate As String
Dim strTmp As String, strItem() As String
Dim i As Date
Open "c:\1.txt" For Input As #1
Line Input #1, strTmp
strItem = Split(Replace(Replace(strTmp, "£o", ":"), "£?", ","), ",")
strTmp = Format(CDate(Trim(strItem(0))), "yyyy-mm-dd HH:nn:ss")
strFirstDate = Left(strTmp, 10) '记录文件中的首日
'将所有存在 8:00 记录的日期记录在 List1 中
List1.Clear
Do Until EOF(1)
If Right(strTmp, 8) = "08:00:00" Then
List1.AddItem Left(strTmp, 10)
End If
If Not EOF(1) Then
Line Input #1, strTmp
strItem = Split(Replace(Replace(strTmp, "£o", ":"), "£?", ","), ",")
strTmp = Format(CDate(Trim(strItem(0))), "yyyy-mm-dd HH:nn:ss")
End If
strLastDate = Left(strTmp, 10) '保存文件中最后日期
Loop
Close #1
'逐日检索是否在 List1 列表中
For i = CDate(strFirstDate) To CDate(strLastDate)
strTmp = Format(i, "yyyy-mm-dd")
If SendMessagebyString(List1.hWND, LB_FINDSTRINGEXACT, -1, strTmp) = -1 Then
Debug.Print strTmp
End If
Next i
End Sub
Option Explicit
Private Sub DateProc(ByVal strDataFile As String)
Dim aIndexTab() As Long
Dim lBaseDate As Long
Dim lLastDate As Long
Dim iFn%, i&, strTemp$
ReDim aIndexTab(0 To 365)
iFn = FreeFile()
Open strDataFile For Input As iFn
Line Input #iFn, strTemp
strTemp = Left$(strTemp, InStr(strTemp, "-") - 1)
lBaseDate = Int(CDate(strTemp & "-1-1"))
lLastDate = Int(CDate(strTemp & "-12-31"))
Seek #iFn, 1
Do While (Not EOF(iFn))
Line Input #iFn, strTemp
If (Len(strTemp)) Then
strTemp = Left$(strTemp, InStr(strTemp, ",") - 1)
If (InStr(strTemp, " 8:00")) Then
aIndexTab(Int(CDate(strTemp)) - lBaseDate) = 1
End If
End If
Loop
Close iFn
'这儿把1年内不含8:00的日期输出:
For i = 0 To lLastDate - lBaseDate
If (aIndexTab(i) = 0) Then Debug.Print Format$(lBaseDate + i, "yyyy-m-d")
Next
MsgBox "处理完毕!", vbInformation
End Sub
Private Sub Command1_Click()
'传入参数就是数据文件的完整路径
Call DateProc("X:\Temp\test.txt")
End Sub