返回列表 上一主題 發帖

[發問] 請問特定欄位的計算

本帖最後由 GBKEE 於 2014-8-16 11:50 編輯

回復 6# bridetobe
試試看
  1. Option Explicit
  2. Sub 讀出()
  3.     Dim a_date, Rng(1 To 2) As Range
  4.     Application.Calculation = xlCalculationManual  '手動計算
  5.     Application.StatusBar = False
  6.     With Sheets("操作")
  7.         Set Rng(1) = .[A2:F2]
  8.         Set Rng(2) = .Cells(1, .Columns.Count)
  9.        .Range(Rng(1)(2, 1), Rng(1).End(xlDown)).Resize(, 7) = ""
  10.         .[B1].Value = InputBox("輸入日期(例2014/7/1):", , "2014/7/1")
  11.         Rng(2) = "日期"
  12.         Rng(2).Offset(1) = .[B1]
  13.         'AdvancedFilter 進階篩選
  14.       '進階篩選:的準則範圍 -> Rng(2).Resize(2)
  15.       Sheets("存貨資料").UsedRange.AdvancedFilter xlFilterCopy, Rng(2).Resize(2), Rng(1)
  16.         Rng(2).EntireColumn = ""
  17.         If Rng(1).End(xlDown).Row <> .Rows.Count Then
  18.             Set Rng(2) = Rng(1).Cells(2, 1)
  19.             Do While Rng(2) <> ""
  20.                 Rng(2).Cells(1, "G") = 上一個小計(.[B1], Rng(2).Text) - Rng(2).Cells(1, "F")
  21.                 If Rng(2).Cells(1, "G") < 0 Then Rng(2).Cells(1, "G") = 0
  22.                  Set Rng(2) = Rng(2).Cells(2)
  23.             Loop
  24.             MsgBox "Ok"            
  25.         Else
  26.             MsgBox "沒有資料"
  27.         End If
  28.     End With
  29.    ' Application.Calculation = xlAutomatic            '自動計算
  30.     '自動篩選後會有工作表重算的動作,程式會等候重算完畢,再繼續執行下去.
  31. End Sub
  32. 'AutoFilter: 自動篩選
  33. Private Function 上一個小計(日期 As String, 品名 As String) As Integer
  34.     Dim Rng As Range
  35.     With Sheets("存貨資料").UsedRange
  36.         '自動計算:當自動篩選後會有工作表重算的動作,影響程式執行的速度
  37.         '自動篩選 指定欄位,準則
  38.         .AutoFilter 1, ">" & 日期
  39.         .AutoFilter 2, "=" & 品名        
  40.         Set Rng = .SpecialCells(xlCellTypeVisible)
  41.         If Rng.Areas(1).Rows.Count > 1 Then
  42.             上一個小計 = Rng.Areas(1).Cells(2, "g")
  43.         Else
  44.             上一個小計 = Rng.Areas(2).Cells(1, "g")
  45.         End If
  46.     End With
  47. End Function
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 口說好話、心想好意、身行好事。
返回列表 上一主題