- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
17#
發表於 2018-1-6 07:18
| 只看該作者
本帖最後由 GBKEE 於 2018-1-6 08:02 編輯
回復 16# afu9240 - Function AGE(D1 As Date) 'Module1(一般模組)
- Application.Volatile (False)
- If IsDate(D1) And D1 > 0 Then
- AGE = DateDiff("m", D1, Date)
- 'AGE = Round(Int(AGE / 12), 0) & "." & AGE - (Round(Int(AGE / 12), 0) * 12) ' 小數點為剩餘月份
- '***此AGE的程式碼中** & "." & ** 傳回文字不能計算,用VAL()函數將文字轉為數字,可計算
- AGE = Val(Round(Int(AGE / 12), 0) & "." & AGE - (Round(Int(AGE / 12), 0) * 12)) ' 小數點為剩餘月份
-
- 'AGE = Round((AGE / 12), 2) '小數點為10進位 '***此AGE傳回數字,可計算
-
- End If
- End Function
複製代碼- Private Sub CommandButton1_Click()
- With Worksheets("人員年資分析表")
- If .AutoFilterMode Then .UsedRange.AutoFilter
- End With
- End
- End Sub
- 'Private Sub CommandButton4_Click() '*****
- Private Sub ComboBox4_Change() '可改用Change不須再按查詢
- Dim Rng As Range, AGE_Average As Double
- 'If ComboBox4.ListIndex = -1 Then MsgBox "請輸入正確的值": Exit Sub
- If ComboBox4.ListIndex = -1 Then Exit Sub '.ListIndex = -1 不在清單的內容
- Set Rng = Worksheets("人員年資分析表").Range("A2")
- If Rng.Parent.AutoFilterMode Then Rng.AutoFilter '取消自動篩選
- Set Rng = Range(Rng, Rng.End(xlToRight).End(xlDown))
- Rng.AutoFilter 1, ComboBox4.Value
- Set Rng = Range(Rng.Cells(2, 1), Rng.End(xlToRight).End(xlDown)).SpecialCells(xlCellTypeVisible)
- AGE_Average = Round(Application.WorksheetFunction.Average(Rng.Columns("G")), 2)
- MsgBox ComboBox4 & " 部門" & vbLf & "平均年齡 " & AGE_Average
-
- With Worksheets("工作表1")
- .Cells.Clear
- Rng.Copy .[a1]
- With .Range("g1", .Range("g1").End(xlDown).Address)
- .Cells(.Count + 1) = "=Average(" & .Cells.Address & ")"
- .Cells(.Count + 1).NumberFormatLocal = "0.00_)"
- End With
- End With
- End Sub
複製代碼 |
|