- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当使用Range.SpecialCells时如果范围不包含与条件匹配的单元格,则会抛出错误,指出未找到单元格。
此问题最常见的解决方案是让它发生并使用错误处理程序来处理它。
这是解决该问题的最有名的方法吗?还是有其他解决方案可能同样好或更好,可以避免使用错误处理程序?
我唯一能想到的就是保存第一个单元格的值,然后将其值更改为与条件匹配的值,这样就可以避免错误,使其始终至少匹配该一个单元格,然后将值更改回其值原始值并检查匹配范围的地址以查看它是否仅匹配一个或多个单元格。
一个糟糕/缓慢的解决方案是根本不使用它,而只是使用带检查的循环。
这里有一些简单的示例代码来演示它如何与错误处理程序一起使用:
Private Sub Procedure()
Dim OriginalRange As Excel.Range
Dim NewRange As Excel.Range
Set OriginalRange = ThisWorkbook.Worksheets(1).Range("A1:C4")
On Error GoTo ErrorHandler
Set NewRange = OriginalRange.SpecialCells(Type:=Excel.XlCellType.xlCellTypeConstants, Value:=Excel.XlSpecialCellsValue.xlNumbers)
Exit Sub
ErrorHandler:
If (VBA.Err.Number <> 1004) Then VBA.Err.Raise VBA.Err.Number
End Sub
最佳答案
是的,使用错误处理程序是完全正常的(我更喜欢这种方式)。我所做的是,将其夹在 On Error Resume Next
和 On Error GoTo 0
之间,然后检查 If NewRange is Nothing
查看此示例
On Error Resume Next
Set NewRange = OriginalRange.SpecialCells(Type:=Excel.XlCellType.xlCellTypeConstants, _
Value:=Excel.XlSpecialCellsValue.xlNumbers)
On Error GoTo 0
If NewRange Is Nothing Then
MsgBox "Your message here informing the USER that desired cells were not found"
Else
'
'~~> Do whatever you want with the range
'
End If
关于excel - 将 Range.SpecialCells 与错误处理程序一起使用是个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55160529/
在我生活的世界中,构建一个将所有飞镖都扔到墙上的解决方案,并希望其中一些能命中靶心是一种非常糟糕的解决方案方法。 那么,我的问题出现了,什么时候在约定可接受的生产系统中使用 INSERT IGNORE
在数据处理时,经常会因为index报错而发愁。不要紧,本次来和大家聊聊pandas中处理索引的几种常用方法。 1.读取时指定索引列 很多情况下,我们的数据源是 CSV 文件。假设
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我是一名优秀的程序员,十分优秀!