Sub Sum1()MsgBox WorksheetFunction.Sum(Sheet1.Range("A1:A10"))End Sub
或:Sub Sum2()...
大家知道大多数的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!”错误,运行上述代码后将产生运行时错误。例如出现类似下图的提示:
excel将工作表按笔画或拼音顺序排序
Excel中没有内置工作表排序的命令或方法,我们可以用VBA来实现工作表排序。下面的VBA代码可以将工作表按其名称的拼音或笔画的顺序来排序,同时还可以指定升序或降序。 Sub SortWorksheets()Dim SortOrd, SortM, ActiveSht As StringDim NumSht()Activ
为避免出现这样的错误,我们可以将单元格中的错误用数值“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用自定义函数获取单元格注释
我们可以用一个自定义函数来提取单元格注释。方法如下: 1.按Alt F11,打开VBA编辑器。 2.单击菜单“插入→模块”,在右边的代码窗口中输入代码: Function GetCommentText(rCommentCell As Range)Dim strGotIt As Stri