| ©«¤l50 ¥DÃD10 ºëµØ0 ¿n¤À69 ÂI¦W0  §@·~¨t²ÎWindows XP ³nÅ骩¥»Office 2007 ¾\ŪÅv20 ©Ê§O¨k ¨Ó¦ÛTaiwan µù¥U®É¶¡2010-6-21 ³Ì«áµn¿ý2015-1-11 
 | 
                
| ¦^´_ 4# hugh0620 
 
 ÁÂÁ±zªºÀ°¦£¡A§Ú·Q§ÚÀ³¸Ó¦b³oÃä¨S¦³¼g¿ù§a....¥H¤U¬°§Úªºcode
 ½Æ»s¥N½XPublic Sub CommandButton1_Click()
Dim FilePath As String, FileName As String
Dim Product As String, Contract As String
Dim Hr As Long, Min As Long, Sec As Long
iYear_s = Val(txtYear_start.Text)
iYear_e = Val(txtYear_end.Text)
iMonth_s = Val(txtMonth_start.Text)
iMonth_e = Val(txtMonth_end.Text)
iDay_s = Val(txtDay_start.Text)
iDay_e = Val(txtDay_end.Text)
If iYear_s = iYear_e Then
    Z = iYear_s
    For J = iMonth_s To iMonth_e
        If J = iMonth_s Then
            For K = iDay_s To 31
        Else
            For K = 1 To iDay_e
        End If
End If
            'transform number into string
            txtYear = Z & ""
            txtMonth = J & ""
            txtDay = K & ""
            FilePath = ThisWorkbook.Path & "\"
             'specify the file name to be "Daily_yyyy_mm_dd.rpt"
            If J < 10 And K < 10 Then
                FileName = "Daily_" & txtYear & "_0" & txtMonth & "_0" & txtDay & ".rpt"
            ElseIf J < 10 And K >= 10 Then
                FileName = "Daily_" & txtYear & "_0" & txtMonth & "_" & txtDay & ".rpt"
            ElseIf J >= 10 And K < 10 Then
                FileName = "Daily_" & txtYear & "_" & txtMonth & "_0" & txtDay & ".rpt"
            ElseIf J >= 10 And K >= 10 Then
                FileName = "Daily_" & txtYear & "_" & txtMonth & "_" & txtDay & ".rpt"
            End If
            Product = txtProduct.Text
            Contract = txtContract.Text
            ' count total number of worksheets
            isheetsnumber = ThisWorkbook.Sheets.Count
            ' free the file number for input
            iFNumber = FreeFile
            ' resume if the .rpt file doesn't exist
On Error GoTo errTrap
            'input .rpt file
            Open FilePath & FileName For Input As #iFNumber
            SheetsName = Mid(FileName, 7, 10) & "f"
            'small loop to test if the worksheet exists, exists =true otherwise false
            For iM = 1 To isheetsnumber
            If ThisWorkbook.Worksheets(iM).Name = SheetsName & "" Then ' &"" transform the number to string
                Sheets_Exist = True: Exit For
            Else
                Sheets_Exist = False
            End If
            Next iM
    'if sheet does not exist, then add one
    If Sheets_Exist = False Then
            Worksheets.Add.Name = SheetsName
            ' move to the end
            ActiveSheet.Move After:=Sheets(isheetsnumber)
            N = 1
            While Not EOF(1)
                Input #iFNumber, A(N, 1), A(N, 2), A(N, 3), A(N, 4), A(N, 5), A(N, 6), A(N, 7), A(N, 8)
                If N = 1 Then
                    Sheets(SheetsName).Cells(1, 1) = A(1, 4)
                    Sheets(SheetsName).Cells(1, 2) = A(1, 5)
                    Sheets(SheetsName).Cells(1, 3) = A(1, 6)
                    N = N + 1
                End If
                If A(N, 2) = Product And A(N, 3) = Contract Then
                    Sec = Val(Left(Right(Str(A(N, 4)), 2), 2))
                    Min = Val(Left(Right(Str(A(N, 4)), 4), 2))
                    Hr = Val(Left(Right(Str(A(N, 4)), 6), 2))
                    A(N, 9) = Str((Hr * 3600 + Min * 60 + Sec) - 31500)
                    Sheets(SheetsName).Cells(N, 1).Value = Val(A(N, 4))
                    Sheets(SheetsName).Cells(N, 2).Value = Val(A(N, 5))
                    Sheets(SheetsName).Cells(N, 3).Value = Val(A(N, 6))
                    Sheets(SheetsName).Cells(N, 4).Value = Val(A(N, 9))
                    N = N + 1
                End If
            Wend
            Close 1
            N = N - 2
     End If
            
            MsgBox ("Total amount of data is " & Str(N))
errTrap:
   Resume Conti
Conti:
        Next K
     Next J
   
End Sub
 | 
 |