合并单元格时使用查找方法,VBA Excel [英] Use find method when there are merged cells, vba excel
问题描述
当在搜索范围中存在合并的单元格时,我有一个关于在Excel VBA中使用 .find
方法的问题.这是代码:
I have a question about using the .find
method in Excel VBA when there is a merged cell in the searching range. Here is the code:
Dim SearchCell As Range
Set SearchCell = ActiveSheet.Range("A:A").Find(What:="Open RMA*", LookIn:=xlValues)
运行代码后SearchCell为空,我猜是因为A26:K26合并为一个单元格,而值为"Open RMA";我不想合并它.
SearchCell is empty after running the code, I guess because A26:K26 is merged as one cell while the value is "Open RMA"; I don't want to unmerge it though.
推荐答案
我认为这是有问题的,除非您扩展范围以包括合并单元格的范围,例如将A:A更改为A:D.
I think this is problematic unless you extend the range to include the merged cell's range, changing A:A to A:D, for example.
另一种方法是:
Dim SearchCell As Range
If Not Application.IsNA(Application.Match("Open RMA*", Range("A:A"), 0)) Then
Set SearchCell = Cells(Application.Match("Open RMA*", Range("A:A"), 0), 1)
MsgBox "Found at " & SearchCell.Address
End If
但是,如果找不到该值,则需要进行一些更好的错误检查.添加是这样的:
It requires some better error checking, though, in case the value is not found. Added like this:
If Not IsError(Application.Match("Open RMA*", Range("A:A"), 0)) Then
Set SearchCell = Cells(Application.Match("Open RMA*", Range("A:A"), 0), 1)
MsgBox "Found at " & SearchCell.Address
End If
这篇关于合并单元格时使用查找方法,VBA Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!