Excel VBA Video Training/ EXCEL DASHBOARD REPORTS

Ozgrid, Experts in Microsoft Excel Spreadsheets

Excel VBA Evaluate Method. Excel Formulas/Functions In Macro Code

 

SEE ALSO: Excel 4.0 EVALUATE Function

Excel VBA WorksheetFunction Property

As you may, or may not know, we can use standard Excel Worksheet Functions in VBA by preceding the Function name with WorksheetFunction or Application. E.g. the example below will sum the range A1:A10.

Sub SumUp()
    MsgBox WorksheetFunction.Sum(Sheet1.Range("A1:A10"))
End Sub



Sub SumUp2()
    MsgBox Application.Sum(Sheet1.Range("A1:A10"))
End Sub

However, IF there ANY Formulae Errors in the range used, it will result in a RunTime Error so you may want to replace all error cells with zero, or at least confirm the range has no formula errors with the SpecialCells Method, like below;

Sub ReplaceErrors()
    On Error Resume Next
      With Sheet1.Range("A1:A10")
           .SpecialCells(xlCellTypeFormulas, xlErrors) = 0
            MsgBox WorksheetFunction.Sum(.Cells)
      End With
    On Error GoTo 0
End Sub


Sub CheckForErrors()
Dim rErrCheck As Range
    On Error Resume Next
      With Sheet1.Range("A1:A10")
           Set rErrCheck = .SpecialCells(xlCellTypeFormulas, xlErrors)
                If Not rErrCheck Is Nothing Then
                   MsgBox "Please fix formula errors in selected cells"
                   Application.Goto .SpecialCells(xlCellTypeFormulas, xlErrors)
                 Else
                    MsgBox WorksheetFunction.Sum(.Cells)
                End If
      End With
    On Error GoTo 0

End Sub

Excel VBA Evaluate Method

With the Evaluate Method, we must still check for, or fix, formula errors in the range we Evaluate with an Excel Formula, but there is less typing and we can simply copy Formulas from the formula bar. The VBA Macros below shows some uses of the Evaluate Method, BUT contain no error checks. Don't forget to add them like above.

Sub EvaluateSum()
    MsgBox Evaluate("SUM(1,2)")
    MsgBox Evaluate("SUM(Sheet1!A1:A10)")
End Sub


Sub EvaluateVlookup()
    MsgBox Evaluate("VLOOKUP(Sheet2!A1,Sheet1!B1:C10,2,FALSE)")
End Sub

Excel Dashboard Reports & Excel Dashboard Charts 50% Off Become an ExcelUser Affiliate & Earn Money

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALL purchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to [email protected] 31 days after purchase date.



Instant Download and Money Back Guarantee on Most Software

Excel VBA Video Training/ EXCEL DASHBOARD REPORTS

Excel Trader Package Technical Analysis in Excel With $139.00 of FREE software!

Microsoft and Microsoft Excel are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft

Some of our more popular products are below...
Convert Excel Spreadsheets To Webpages | Trading In Excel | Construction Estimators | Finance Templates & Add-ins Bundle | Code-VBA | Smart-VBA | Print-VBA | Excel Data Manipulation & Analysis | Convert MS Office Applications To...... | Analyzer Excel | Downloader Excel | MSSQL Migration Toolkit | Monte Carlo Add-in | Excel Costing Templates