ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ½Ð°Ý¸Ó¦p¦ó§å¦¸¶×¤J¤£¦P¤é´Á¸ê®Æ¦ÜEXCEL¡H

[µo°Ý] ½Ð°Ý¸Ó¦p¦ó§å¦¸¶×¤J¤£¦P¤é´Á¸ê®Æ¦ÜEXCEL¡H

¦U¦ì¦n¡A

¤£¦n·N«ä¤S¨Ó³Â·Ð¤j®a¤F¡C³Ìªñ¦b¸ÕµÛ§ï¼g¥H¡iµ{¦¡¥æ©ö»EÄ_¬Ö¡j¸Ì­±CLCY¤j¤À¨Éªº´Á¥æ©ÒÂàÀÉÀɮסA¸ÕµÛ§â­ì¥ý¥u¯à¤@¦¸¿é¤J¤@¤ÑªºEXCEL§ï¼g¦¨¥i¥H§å¦¸¿é¤J¦h¤Ñªºª©¥»¡]¤ñ¦p»¡±q¤@­Ó¦X¬ù¤ë¥÷ªº¶}­Ü¤é¦Üµ²ºâ¤é¡^¡A¦ý¬O¤@ª½¤£¦¨¥\¡C
¤]´N¬O»¡§Ú·Q­n±q´Á¥æ©Ò´£¨Ñªº½L«á¨Cµ§¥æ©ö¸ê®Æ¡].rptÀÉ¡^§å¦¸¿é¤J¦ÜExcel¤¤¡]Excel 2010)¡A¤ñ¦p»¡±q2011-12-15, 2011-12-16,......2012-01-12, 2012-01-13 µ¥µ¥

½Ð¥ýÅý§Úªþ¤W§Úªºcode¡A¦A»¡©ú§Úªº°ÝÃD¡C
  1. Public Sub CommandButton1_Click()
  2. Dim FilePath As String, FileName As String

  3. Dim Product As String, Contract As String
  4. Dim Hr As Long, Min As Long, Sec As Long

  5. iYear_s = Val(txtYear_start.Text)
  6. iYear_e = Val(txtYear_end.Text)
  7. iMonth_s = Val(txtMonth_start.Text)
  8. iMonth_e = Val(txtMonth_end.Text)
  9. iDay_s = Val(txtDay_start.Text)
  10. iDay_e = Val(txtDay_end.Text)

  11. For Z = iYear_s To iYear_e
  12.     For J = iMonth_s To iMonth_e
  13.         For K = iDay_s To iDay_e
  14.             'transform number into string
  15.             txtYear = Z & ""
  16.             txtMonth = J & ""
  17.             txtDay = K & ""
  18.             FilePath = ThisWorkbook.Path & "\"
  19.              'specify the file name to be "Daily_yyyy_mm_dd.rpt"
  20.             If J < 10 And K < 10 Then
  21.                 FileName = "Daily_" & txtYear & "_0" & txtMonth & "_0" & txtDay & ".rpt"
  22.             ElseIf J < 10 And K >= 10 Then
  23.                 FileName = "Daily_" & txtYear & "_0" & txtMonth & "_" & txtDay & ".rpt"
  24.             ElseIf J >= 10 And K < 10 Then
  25.                 FileName = "Daily_" & txtYear & "_" & txtMonth & "_0" & txtDay & ".rpt"
  26.             ElseIf J >= 10 And K >= 10 Then
  27.                 FileName = "Daily_" & txtYear & "_" & txtMonth & "_" & txtDay & ".rpt"
  28.             End If
  29.             Product = txtProduct.Text
  30.             Contract = txtContract.Text
  31.             ' count total number of worksheets
  32.             isheetsnumber = ThisWorkbook.Sheets.Count
  33.             ' free the file number for input
  34.             iFNumber = FreeFile
  35.             ' resume if the .rpt file doesn't exist
  36. On Error GoTo errTrap
  37.             'input .rpt file
  38.             Open FilePath & FileName For Input As #iFNumber
  39.             SheetsName = Mid(FileName, 7, 10) & "f"
  40.             'small loop to test if the worksheet exists, exists =true otherwise false
  41.             For iM = 1 To isheetsnumber
  42.             If ThisWorkbook.Worksheets(iM).Name = SheetsName & "" Then ' &"" transform the number to string
  43.                 Sheets_Exist = True: Exit For
  44.             Else
  45.                 Sheets_Exist = False
  46.             End If
  47.             Next iM
  48.     'if sheet does not exist, then add one
  49.     If Sheets_Exist = False Then
  50.             Worksheets.Add.Name = SheetsName
  51.             ' move to the end
  52.             ActiveSheet.Move After:=Sheets(isheetsnumber)
  53.             N = 1
  54.             While Not EOF(1)
  55.                 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)
  56.                 If N = 1 Then
  57.                     Sheets(SheetsName).Cells(1, 1) = A(1, 4)
  58.                     Sheets(SheetsName).Cells(1, 2) = A(1, 5)
  59.                     Sheets(SheetsName).Cells(1, 3) = A(1, 6)
  60.                     N = N + 1
  61.                 End If
  62.                 If A(N, 2) = Product And A(N, 3) = Contract Then
  63.                     Sec = Val(Left(Right(Str(A(N, 4)), 2), 2))
  64.                     Min = Val(Left(Right(Str(A(N, 4)), 4), 2))
  65.                     Hr = Val(Left(Right(Str(A(N, 4)), 6), 2))
  66.                     A(N, 9) = Str((Hr * 3600 + Min * 60 + Sec) - 31500)
  67.                     Sheets(SheetsName).Cells(N, 1).Value = Val(A(N, 4))
  68.                     Sheets(SheetsName).Cells(N, 2).Value = Val(A(N, 5))
  69.                     Sheets(SheetsName).Cells(N, 3).Value = Val(A(N, 6))
  70.                     Sheets(SheetsName).Cells(N, 4).Value = Val(A(N, 9))
  71.                     N = N + 1
  72.                 End If
  73.             Wend
  74.             Close 1
  75.             N = N - 2
  76.      End If
  77.             
  78.             MsgBox ("Total amount of data is " & Str(N))
  79. errTrap:
  80.    Resume Conti

  81. Conti:
  82.         Next K
  83.      Next J
  84.    
  85. Next Z
  86. End Sub
½Æ»s¥N½X
¦pªG¬O¦P¤@­Ó¤ë¥÷©Î¬O¦~¥÷ªº¸Ü¨S¦³°ÝÃD¡A¦ý¬O¥X²{¸ó¤ëªº¸Ü¡A´N¥X°ÝÃD¤F¡C¥D­n¬O¦]¬°¤@¦ý¸ó¤ë¡A¤é´Á´N·|±q1¸¹¶}©l¡A¦bFor Next ¦^°é¸Ì­±¨S¿ìªk³B²z¡A§Ú´¿¸g¤]¬OµÛ¥Îblock Ifªº§PÂ_¦¡·Q¥h¤À¿ë¤£¦Pªº±¡ªp¡A¦ý¬O¦n¹³¥ÎIf ¤§«á¨S¿ìªk¥ÎFor ³o¼Ëexcel·|§i¶D§Ú¡§¦³If ¦ý¨S¦³End If"

½Ð°Ý¦³¨S¦³¿ìªk¸Ñ¨M¡]©Î§ï¼g¡^³o­Ó°ÝÃD©O¡H ÁÂÁ¦U¦ì

PS: ­ì©lÀɮצ³ÂI¹L¤j¡AµLªkªþ¥[¡A³Â·Ð¦Ü http://min.us/mx0lbUjTp ¤U¸ü ¡]ÂàÀɳ¡¥÷ªºCode§@ªÌ¬°¡iµ{¦¡¥æ©ö»EÄ_¬Ö¡jªºCLCY¤j¡^
§Ú·R³Â»¶®a±Ú°Q½×°Ï¡I¡I

¦^´_ 1# j1221

     ¦ý¬O¦n¹³¥ÎIf ¤§«á¨S¿ìªk¥ÎFor ³o¼Ëexcel·|§i¶D§Ú¡§¦³If ¦ý¨S¦³End If"
      ¼Ó¥D~ ³o­Ó°ÝÃD¬O±z¦b¼¶¼g®É~ ¦³¼gIF ¦ý¨S¦³¼gµ²§ÀEed IF~      
     ¼Ó¥DªºÆ[©Àªº¹ïªº§r~­Y¬O¸Ø¤ë~ §Úªº¼gªk~ ª½Ä±¬O¥ÎIF¥h§PÂ_~
      ¦]¦¹~ ¼Ó¥D ­n¤£­n¦A¸Õ¤@¤U~ §A­ì©lªº¼gªk~ À³¸Ó´N¥i¥H¸Ñ¨M¸ó¤ëªº°ÝÃD~
¾Ç²ß¤~¯à´£¤É¦Û¤v

TOP

¦^´_ 2# hugh0620

ÁÂÁ±zªº¦^ÂСA¦ý¬O¨ä¹ê§Ú´N¬O§Q¥Î³o¼Ë¼gªº¡A§Úªº¤èªk¦p¤U
  1. If iYear_s = iYear_e Then
  2.     Z = iYear_s
  3.     For J = iMonth_s To iMonth_e
  4.         If J = iMonth_s Then
  5.             For K = iDay_s To 31
  6.         Else
  7.             For K = 1 To iDay_e
  8.         End If
  9. End If
½Æ»s¥N½X
Excel ¸õ¥XªºÁÙ¬O ¡§¦³Else ¨S¦³If¡§ ½Ð°Ý°ÝÃD¬O¥X¦b­þ¸Ì©O¡H¡H
§Ú·R³Â»¶®a±Ú°Q½×°Ï¡I¡I

TOP

¥»©«³Ì«á¥Ñ hugh0620 ©ó 2012-5-17 09:27 ½s¿è

¦^´_ 3# j1221


    §A¦³¼gfor ¦ý¬O¤Ö¤Fnext~ ·íµM·|²£¥Í¿ù»~§r~
    ­n¦bÀ˵ø¤@¤U~ §A¼gªº¬O¤£¬O¤@¹ï¤@¹ïªº~
    if  
         for
               for
                           if    
                           end  if
               next
         next
     end if

Àˬd¹L§Aµo¤åªºµ{¦¡½X~ ¨S¦³°ÝÃD~

­n¤£­n§â§A­×§ï¹L«áªº§¹¾ãµ{¦¡½Xpo¤W¨Ó¬Ý¬Ý©O
¾Ç²ß¤~¯à´£¤É¦Û¤v

TOP

¦^´_ 4# hugh0620


    ÁÂÁ±zªºÀ°¦£¡A§Ú·Q§ÚÀ³¸Ó¦b³oÃä¨S¦³¼g¿ù§a....¥H¤U¬°§Úªºcode
  1. Public Sub CommandButton1_Click()
  2. Dim FilePath As String, FileName As String

  3. Dim Product As String, Contract As String
  4. Dim Hr As Long, Min As Long, Sec As Long

  5. iYear_s = Val(txtYear_start.Text)
  6. iYear_e = Val(txtYear_end.Text)
  7. iMonth_s = Val(txtMonth_start.Text)
  8. iMonth_e = Val(txtMonth_end.Text)
  9. iDay_s = Val(txtDay_start.Text)
  10. iDay_e = Val(txtDay_end.Text)

  11. If iYear_s = iYear_e Then
  12.     Z = iYear_s
  13.     For J = iMonth_s To iMonth_e
  14.         If J = iMonth_s Then
  15.             For K = iDay_s To 31
  16.         Else
  17.             For K = 1 To iDay_e
  18.         End If
  19. End If
  20.             'transform number into string
  21.             txtYear = Z & ""
  22.             txtMonth = J & ""
  23.             txtDay = K & ""
  24.             FilePath = ThisWorkbook.Path & "\"
  25.              'specify the file name to be "Daily_yyyy_mm_dd.rpt"
  26.             If J < 10 And K < 10 Then
  27.                 FileName = "Daily_" & txtYear & "_0" & txtMonth & "_0" & txtDay & ".rpt"
  28.             ElseIf J < 10 And K >= 10 Then
  29.                 FileName = "Daily_" & txtYear & "_0" & txtMonth & "_" & txtDay & ".rpt"
  30.             ElseIf J >= 10 And K < 10 Then
  31.                 FileName = "Daily_" & txtYear & "_" & txtMonth & "_0" & txtDay & ".rpt"
  32.             ElseIf J >= 10 And K >= 10 Then
  33.                 FileName = "Daily_" & txtYear & "_" & txtMonth & "_" & txtDay & ".rpt"
  34.             End If
  35.             Product = txtProduct.Text
  36.             Contract = txtContract.Text
  37.             ' count total number of worksheets
  38.             isheetsnumber = ThisWorkbook.Sheets.Count
  39.             ' free the file number for input
  40.             iFNumber = FreeFile
  41.             ' resume if the .rpt file doesn't exist
  42. On Error GoTo errTrap
  43.             'input .rpt file
  44.             Open FilePath & FileName For Input As #iFNumber
  45.             SheetsName = Mid(FileName, 7, 10) & "f"
  46.             'small loop to test if the worksheet exists, exists =true otherwise false
  47.             For iM = 1 To isheetsnumber
  48.             If ThisWorkbook.Worksheets(iM).Name = SheetsName & "" Then ' &"" transform the number to string
  49.                 Sheets_Exist = True: Exit For
  50.             Else
  51.                 Sheets_Exist = False
  52.             End If
  53.             Next iM
  54.     'if sheet does not exist, then add one
  55.     If Sheets_Exist = False Then
  56.             Worksheets.Add.Name = SheetsName
  57.             ' move to the end
  58.             ActiveSheet.Move After:=Sheets(isheetsnumber)
  59.             N = 1
  60.             While Not EOF(1)
  61.                 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)
  62.                 If N = 1 Then
  63.                     Sheets(SheetsName).Cells(1, 1) = A(1, 4)
  64.                     Sheets(SheetsName).Cells(1, 2) = A(1, 5)
  65.                     Sheets(SheetsName).Cells(1, 3) = A(1, 6)
  66.                     N = N + 1
  67.                 End If
  68.                 If A(N, 2) = Product And A(N, 3) = Contract Then
  69.                     Sec = Val(Left(Right(Str(A(N, 4)), 2), 2))
  70.                     Min = Val(Left(Right(Str(A(N, 4)), 4), 2))
  71.                     Hr = Val(Left(Right(Str(A(N, 4)), 6), 2))
  72.                     A(N, 9) = Str((Hr * 3600 + Min * 60 + Sec) - 31500)
  73.                     Sheets(SheetsName).Cells(N, 1).Value = Val(A(N, 4))
  74.                     Sheets(SheetsName).Cells(N, 2).Value = Val(A(N, 5))
  75.                     Sheets(SheetsName).Cells(N, 3).Value = Val(A(N, 6))
  76.                     Sheets(SheetsName).Cells(N, 4).Value = Val(A(N, 9))
  77.                     N = N + 1
  78.                 End If
  79.             Wend
  80.             Close 1
  81.             N = N - 2
  82.      End If
  83.             
  84.             MsgBox ("Total amount of data is " & Str(N))
  85. errTrap:
  86.    Resume Conti

  87. Conti:
  88.         Next K
  89.      Next J
  90.    

  91. End Sub
½Æ»s¥N½X
§Ú·R³Â»¶®a±Ú°Q½×°Ï¡I¡I

TOP

¥»©«³Ì«á¥Ñ hugh0620 ©ó 2012-5-18 08:21 ½s¿è

¦^´_ 5# j1221

     ÁÙ¯uªº¼g¿ù¤F­ò~
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
            
            NEXT K  

      Else
            For K = 1 To iDay_e

            next K

        End If
    next J
End If

«ö±z­ì©lªº¼gªk~ °õ¦æ¨ìFor J = iMonth_s To iMonth_e ³o¤@¥y~
¦p¦ó¸õ¨ìnext J©O~  ¤£¥i¯à­ò~ ¤¤¶¡¦³END IF
¦bµ{¦¡°õ¦æ®É~ ´N·|¿ù»~¤F~
§A¥i¥H¦bIF .....   END IF ¤¤¶¡©ñ¦bFor  ...  Next
¦ý¤£¯à§â  next ©ñ¦b end  if«á­±~
¾Ç²ß¤~¯à´£¤É¦Û¤v

TOP

¦^´_ 6# hugh0620

ÁÂÁ±zªº¦^ÂÐ!!

·íªì§Ú¬O·Q­n¥ý§PÂ_¤§«á¦A¶}©l¦^°é¡A¤]´N¬O»¡§Ú¤£·Q½Æ»s¨â¦¸­«½Æªºcode (¶i¤Jfor¤§«áªº¨º³¡¤À) ¡A¦]¬°³o¼Ë¾ã­Óµ{§Ç·|Åܱo«Üªø¡C

½Ð°Ý¦³¿ìªk°µ¨ì³o¼Ë¶Ü?
§Ú·R³Â»¶®a±Ú°Q½×°Ï¡I¡I

TOP

        ÀR«ä¦Û¦b : ­n¥Î¤ß¡A¤£­n¾Þ¤ß¡B·Ð¤ß¡C
ªð¦^¦Cªí ¤W¤@¥DÃD