Microsoft Excel是Microsoft为使用Windows和Apple Macintosh操作系统的电脑编写的一款电子表格软件。直观的界面、出色的计算功能和图表工具,再加上成功的市场营销,使Excel成为最流行的个人计算机数据处理软件。 大家知道大多数的Excel工作表函数可以用在VBA中,通过下面的方法来调用,例如对A1:A10单元格求和: Sub Sum1() MsgBox WorksheetFunction.Sum(Sheet1.Range("A1:A10")) End Sub 或: Sub Sum2() MsgBox Application.Sum(Sheet1.Range("A1:A10")) End Sub 但是如果在单元格中包含错误,例如上例中的A1:A10区域包含一个“#DIV/0!”错误,运行上述代码后将产生运行时错误。例如出现类似下图的提示: 为避免出现这样的错误,我们可以将单元格中的错误用数值“0”取代。用下面的代码: 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 "指定的单元格中包含错误!" Application.Goto .SpecialCells(xlCellTypeFormulas, xlErrors) Else MsgBox WorksheetFunction.Sum(.Cells) End If End With On Error GoTo 0 End Sub
Excel整体界面趋于平面化,显得清新简洁。流畅的动画和平滑的过渡,带来不同以往的使用体验。 |