合并单元格时使用find方法,vba excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17604353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:11:14  来源:igfitidea点击:

Use find method when there are merged cells, vba excel

excelvbamergefind

提问by user2495069

I have a question about using the .findmethod in Excel VBA when there is a merged cell in the searching range. Here is the code:

.find当搜索范围中有合并单元格时,我有一个关于在 Excel VBA 中使用该方法的问题。这是代码:

Dim SearchCell As Range
Set SearchCell = ActiveSheet.Range("A:A").Find(What:="Open RMA*", LookIn:=xlValues)

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.

运行代码后SearchCell为空,我猜是因为A26:K26被合并为一个单元格而值为“Open RMA”;不过,我不想取消合并。

回答by Andy G

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.

我认为这是有问题的,除非您扩展范围以包括合并单元格的范围,例如将 A:A 更改为 A:D。

An alternative approach would be:

另一种方法是:

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. Addedlike 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