VBA/Excel/Access/Word/Excel/Excel Buildin Function

Материал из VB Эксперт
Перейти к: навигация, поиск

Calculate a moving average

 
Option Explicit
Public Sub MoveAverage()
  Dim aRange As Range
  Dim i As Long
  
  Set aRange = Range("B1:B3")
  For i = 3 To 12
    Cells(i, "C").Value = WorksheetFunction.Round(WorksheetFunction.Sum(aRange) / 3, 0)
    Set aRange = aRange.Offset(1, 0)
  Next i
End Sub



Call worksheet function directly

 
Sub refStatse()
    Debug.Print "MIN: " & Application.WorksheetFunction.Min(Range("A1:A10"))
    Debug.Print "MAX: " & Application.WorksheetFunction.Max(Range("A1:A10"))
    Debug.Print "SUM: " & Application.WorksheetFunction.Sum(Range("A1:A10"))
    Debug.Print "AVG: " & Format(Application.WorksheetFunction.Average(Range("A1:A10")), "#0.00")
End Sub



Cleans up every selected cell: with Proper function

 
     Sub FixTextInAllCells()
         Dim Cell
         For Each Cell In Selection
             Cell.Value = Application.WorksheetFunction.Proper(Cell.Value)
         Next
     End Sub



Payment Function:Pmt (interest, term, principal, future value, type)

 
Sub loanPayment()
   Dim dblRate As Double
   Dim intTerm As Integer
   Dim dblPrincipal As Double
   Dim dblPayment As Double
   dblRate = 0.075
   intTerm = 5
   dblPrincipal = 7000
   dblPayment = Pmt(dblRate / 12, intTerm * 12, -dblPrincipal)
   msgBox "The monthly payment is: " & dblPayment
End Sub



Sum a Range

 
Public Sub SumRangeTest()
  Dim aRange As Range
  
  With ActiveCell
    Set aRange = Range(.Offset(1), .Offset(1).End(xlDown))
    .Formula = "=SUM(" & aRange.Address(RowAbsolute:=False,ColumnAbsolute:=False) & ")"
    .Copy Destination:=Range(.Cells(1), .Offset(1).End(xlToRight).Offset(-1))
  End With
  
End Sub



Use function from Excel

 
Sub from()
     MsgBox [MOD(TODAY(),7)]
End Sub



Using Excel Application Functions in VBA

 
Sub func()
    Debug.Print Application.WorksheetFunction.Power(2, 3)
    Debug.Print Application.WorksheetFunction.Average(5, 7, 9)
    Debug.Print Application.WorksheetFunction.StDev(3, 7, 11)
End Sub