Board logo

標題: [發問] 請教改良方法,謝謝~ [打印本頁]

作者: 198188    時間: 2012-12-12 09:25     標題: 請教改良方法,謝謝~

  1. Sub Detail()
  2. Dim FRng As Range
  3. Dim A As Range, Rng As Range
  4. Dim i As Integer
  5. Dim LastRec As Integer
  6. Dim k As Integer
  7. Dim j As Integer
  8.    
  9.     k = Sheets("state").Range("A1").CurrentRegion.Rows.Count
  10.          For j = 2 To k
  11.      If IsError(Application.VLookup(Worksheets("state").Range("A" & j).Value, Sheets("Result").Range("A:B"), 1, False)) Then
  12.              Sheets("State").Cells(j, "B") = Worksheets("Result").Range("S" & j).Value
  13.              Sheets("State").Cells(j, "O") = Worksheets("Result").Range("L" & j).Value
  14.              Sheets("State").Cells(j, "F") = Worksheets("Result").Range("E" & j).Value
  15.              Sheets("State").Cells(j, "K") = Worksheets("Result").Range("U" & j).Value
  16.              Sheets("State").Cells(j, "L") = Worksheets("Result").Range("V" & j).Value
  17.              Sheets("State").Cells(j, "P") = Worksheets("Result").Range("AL" & j).Value
  18.              Sheets("State").Cells(j, "Q") = Worksheets("Result").Range("Q" & j).Value
  19.              Sheets("State").Cells(j, "R") = Worksheets("Result").Range("M" & j).Value
  20.              Sheets("State").Cells(j, "S") = Worksheets("Result").Range("N" & j).Value
  21.              Sheets("State").Cells(j, "T") = Worksheets("Result").Range("X" & j).Value
  22.              Sheets("State").Cells(j, "U") = Worksheets("Result").Range("Z" & j).Value
  23.              Sheets("State").Cells(j, "W") = Worksheets("Result").Range("K" & j).Value
  24.              Sheets("State").Cells(j, "X") = Worksheets("Result").Range("C" & j).Value
  25.             
  26.             End If
  27.        Next

  28. fs = "W:\PIHK\NEW香港辦公室正本收放單記錄FROM 01-MAR-2012 to current(updated).xlsx"
  29. Set Wb = Workbooks.Open(fs)
  30. With ThisWorkbook.Worksheets("State")
  31. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  32.      Set FRng = Wb.Sheets("收單記錄").Range("C:C").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  33.      If Not FRng Is Nothing Then
  34.            A.Offset(, 9) = FRng.Offset(, 4).Value
  35.            If Rng Is Nothing Then Set Rng = A.Offset(, 7) Else Set Rng = Union(Rng, A.Offset(, 7))
  36.         End If
  37.            Set FRng = Nothing
  38. Next
  39. End With
  40. Wb.Close 0

  41.   fs = "W:\PIHK\NEW香港辦公室正本收放單記錄FROM 01-MAR-2012 to current(updated).xlsx"
  42. Set Wb = Workbooks.Open(fs)
  43. With ThisWorkbook.Worksheets("State")
  44. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  45.      Set FRng = Wb.Sheets("放單記錄").Range("C:C").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  46.      If Not FRng Is Nothing Then
  47.            A.Offset(, 21) = FRng.Offset(, 4).Value
  48.            If Rng Is Nothing Then Set Rng = A.Offset(, 21) Else Set Rng = Union(Rng, A.Offset(, 21))
  49.         End If
  50.            Set FRng = Nothing
  51. Next
  52. End With
  53. Wb.Close 0

  54. fs = "W:\Payment Daily Report\Brazil shipment schedule.xlsx"
  55.   Set Wb = Workbooks.Open(fs)
  56. With ThisWorkbook.Worksheets("State")
  57. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  58.      Set FRng = Wb.Sheets("sheet1").Range("D:D").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  59.      If Not FRng Is Nothing Then
  60.            A.Offset(, 7) = FRng.Offset(, 10).Value
  61.            If Rng Is Nothing Then Set Rng = A.Offset(, 5) Else Set Rng = Union(Rng, A.Offset(, 5))
  62.         End If
  63.            Set FRng = Nothing
  64. Next
  65. End With
  66. Wb.Close 0

  67. fs = "W:\Payment Daily Report\daily doc.xlsx"

  68. 'fs = ThisWorkbook.Path & "\payment report 2012.xlsx"
  69. Set Wb = Workbooks.Open(fs)
  70. With ThisWorkbook.Worksheets("State")
  71. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  72.      Set FRng = Wb.Sheets("DAILY").Range("D:D").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  73.      If Not FRng Is Nothing Then
  74.            A.Offset(, 13) = FRng.Offset(, 3).Value
  75.            If Rng Is Nothing Then Set Rng = A.Offset(, 11) Else Set Rng = Union(Rng, A.Offset(, 11))
  76.         End If
  77.            Set FRng = Nothing
  78. Next
  79. End With
  80. Wb.Close 0

  81.   fs = "W:\Payment Daily Report\JPMHK RECEIVED RECORD.xlsx"
  82. Set Wb = Workbooks.Open(fs)
  83. With ThisWorkbook.Worksheets("State")
  84. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  85.      Set FRng = Wb.Sheets("NEW TC ITEMS").Range("C:C").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  86.      If Not FRng Is Nothing Then
  87.            A.Offset(, 8) = FRng.Offset(, 10).Value
  88.            If Rng Is Nothing Then Set Rng = A.Offset(, 6) Else Set Rng = Union(Rng, A.Offset(, 6))
  89.         End If
  90.            Set FRng = Nothing
  91. Next
  92. End With
  93. Wb.Close 0

  94.    fs = "W:\Payment Daily Report\Outstanding Payments.xlsm"
  95. Set Wb = Workbooks.Open(fs)
  96. With ThisWorkbook.Worksheets("State")
  97. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  98.      Set FRng = Wb.Sheets("outstanding payments").Range("A:A").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  99.      If Not FRng Is Nothing Then
  100.            A.Offset(, 6) = FRng.Offset(, 4).Value
  101.            If Rng Is Nothing Then Set Rng = A.Offset(, 4) Else Set Rng = Union(Rng, A.Offset(, 6))
  102.         End If
  103.        Set FRng = Nothing
  104. Next
  105. End With
  106. Wb.Close 0

  107.     fs = "W:\Payment Daily Report\payment report.xlsx"
  108. Set Wb = Workbooks.Open(fs)
  109. With ThisWorkbook.Worksheets("State")
  110. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  111.      Set FRng = Wb.Sheets("New form of payment report").Range("B:B").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  112.      If Not FRng Is Nothing Then
  113.            A.Offset(, 12) = FRng.Offset(, 7).Value
  114.            If Rng Is Nothing Then Set Rng = A.Offset(, 10) Else Set Rng = Union(Rng, A.Offset(, 10))
  115.         End If
  116.            Set FRng = Nothing
  117. Next
  118. End With
  119. Wb.Close 0


  120. fs = "W:\Payment Daily Report\HK ETA update.xlsx"
  121. Set Wb = Workbooks.Open(fs)
  122. With ThisWorkbook.Worksheets("State")
  123. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  124.      Set FRng = Wb.Sheets("香港&海防單").Range("A:A").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  125.      If Not FRng Is Nothing Then
  126.            A.Offset(, 2) = FRng.Offset(, 11).Value
  127.            If Rng Is Nothing Then Set Rng = A.Offset(, 2) Else Set Rng = Union(Rng, A.Offset(, 2))
  128.         End If
  129.            Set FRng = Nothing
  130. Next
  131. End With
  132. Wb.Close 0

  133.   fs = "W:\Payment Daily Report\Mainland ETA Update.xlsx"
  134. Set Wb = Workbooks.Open(fs)
  135. With ThisWorkbook.Worksheets("State")
  136. For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  137.      Set FRng = Wb.Sheets("MAILAND ETA").Range("A:A").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  138.      If Not FRng Is Nothing Then
  139.            A.Offset(, 3) = FRng.Offset(, 9).Value
  140.            If Rng Is Nothing Then Set Rng = A.Offset(, 3) Else Set Rng = Union(Rng, A.Offset(, 3))
  141.         End If
  142.            Set FRng = Nothing
  143. Next
  144. End With
  145. Wb.Close 0

  146. End Sub
複製代碼
另外下面這兩個可否改成如果在"W:\Payment Daily Report\HK ETA update.xlsx"Sheets("香港&海防單") 內找到A.Offset(, 2) = FRng.Offset(, 11).Value,如果找不到就在"W:\Payment Daily Report\Mainland ETA Update.xlsx" Sheets("MAILAND ETA")內找,如果找到就A.Offset(, 2) = FRng.Offset(, 9).Value,如果兩個都找不到就將A.Offset(, 2) 就不變,維持原本的資料。

fs = "W:\Payment Daily Report\HK ETA update.xlsx"
Set Wb = Workbooks.Open(fs)
With ThisWorkbook.Worksheets("State")
For Each A In .Range(.[A2], .Range("A1").End(xlDown))
     Set FRng = Wb.Sheets("香港&海防單").Range("A:A").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
     If Not FRng Is Nothing Then
           A.Offset(, 2) = FRng.Offset(, 11).Value
           If Rng Is Nothing Then Set Rng = A.Offset(, 2) Else Set Rng = Union(Rng, A.Offset(, 2))
        End If
           Set FRng = Nothing
Next
End With
Wb.Close 0

  fs = "W:\Payment Daily Report\Mainland ETA Update.xlsx"
Set Wb = Workbooks.Open(fs)
With ThisWorkbook.Worksheets("State")
For Each A In .Range(.[A2], .Range("A1").End(xlDown))
     Set FRng = Wb.Sheets("MAILAND ETA").Range("A:A").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
     If Not FRng Is Nothing Then
           A.Offset(, 3) = FRng.Offset(, 9).Value
           If Rng Is Nothing Then Set Rng = A.Offset(, 3) Else Set Rng = Union(Rng, A.Offset(, 3))
        End If
           Set FRng = Nothing
Next
End With
Wb.Close 0
作者: mark15jill    時間: 2012-12-12 10:51

回復 1# 198188

    在下提供幾個小建議..
1.
可以將 Worksheets("Result") 設為定數 ( 如 KK= Worksheets("Result"))
這樣的話

  1.              .Cells(j, "B") = KK.Range("S" & j).Value
  2.              .Cells(j, "O") = KK.Range("L" & j).Value
  3.              .Cells(j, "F") = KK.Range("E" & j).Value
  4.              .Cells(j, "K") = KK.Range("U" & j).Value
  5.              .Cells(j, "L") = KK.Range("V" & j).Value
  6.              .Cells(j, "P") = KK.Range("AL" & j).Value
  7.              .Cells(j, "Q") = KK.Range("Q" & j).Value
  8.              .Cells(j, "R") = KK.Range("M" & j).Value
  9.              .Cells(j, "S") = KK.Range("N" & j).Value
  10.              .Cells(j, "T") = KK.Range("X" & j).Value
  11.              .Cells(j, "U") = KK.Range("Z" & j).Value
  12.              .Cells(j, "W") = KK.Range("K" & j).Value
  13.              .Cells(j, "X") = KK.Range("C" & j).Value
複製代碼
2.開啟、複製檔案部份
稍微看了一下 有更動的地方僅( fs 、thisworkbook.worksheets("xxx") 、 A.Offset(,X))
這樣的話可以 考慮用 Select Case ... End Select 作替代 類似以下方法
  1. '如有9項
  2. for uuw = 1 to 9
  3.         select case uuw
  4.                 case 1
  5.                      fs = "W:\PIHK\NEW香港辦公室正本收放單記錄FROM 01-MAR-2012 to current(updated).xlsx"
  6.                      Set Wb = Workbooks.Open(fs)
  7.                      With ThisWorkbook.Worksheets("State")
  8.                             For Each A In .Range(.[A2], .Range("A1").End(xlDown))
  9.                                  Set FRng = Wb.Sheets("收單記錄").Range("C:C").Find(A, lookat:=xlWhole, SearchDirection:=xlPrevious)
  10.                                  If Not FRng Is Nothing Then
  11.                                       A.Offset(, 9) = FRng.Offset(, 4).Value
  12.                                       If Rng Is Nothing Then Set Rng = A.Offset(, 7) Else Set Rng = Union(Rng, A.Offset(, 7))
  13.                                       Set FRng = Nothing
  14.                                  end if
  15.                            Next
  16.                       End With


  17.                 case 2
  18.                 case 3
  19.                 case 4
  20.                 case 5
  21.                 case 6
  22.                 case 7
  23.                 case 8
  24.                 case 9       

  25.         end select
  26.                       Wb.Close 0
  27. next uuw
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)