Monday, June 25, 2012

Using Worksheet function for VBA code



The  macro sums the salaries for employees in a specific department and a specific location

Sub calcSalaries()
Dim objDept As Range, objLoc As Range, objSal As Range
Dim strDept, strLoc As String, cursum As Currency
Sheets("Employees").Activate
With ActiveCell.CurrentRegion
    Set objDept = .Columns(4)
    Set objLoc = .Columns(5)
    Set objSal = .Columns(6)
    strDept = InputBox(Prompt:="Which department(cancel or blank for all departments)?", Default:="Finance")
    If strDept = vbNullString Then strDept = "*"
    strLoc = InputBox(Prompt:="Which department(cancel or blank for all departments)?", Default:="New Delhi")
    If strLoc = vbNullString Then strLoc = "*"
    cursum = WorksheetFunction.SumIfs(objSal, objDept, strDept, objLoc, strLoc)
    MsgBox cursum
    MsgBox "The total for" & strDept & " in" & strLoc & " is:" & FormatCurrency(cursum)
End With


End Sub

No comments:

Post a Comment