Range.Find() 用于在 Excel VBA 中查找带有回车符的文本。

3
我的目标
找到包含唯一字符串的标题单元格所在的列。换句话说,我知道单元格的文本,我知道单元格位于第一行,但我不知道是哪一列。注意:我想要搜索整个文本,而不仅仅是其中一部分。注意2:文本可能会变化,所以我不能将值硬编码到我的代码中。相反,我需要使用存储该值的变量。 问题所在
当标题文本中没有换行符时,简单的newCol = Range("1:1").Find(headerText).Column可以正常工作。然而,如果有一个换行符,这就不起作用了。它会抛出错误“对象变量或With块变量未设置”。以下是我的确切标题字符串:
Incomplete Email
(more text)

我已经尝试过的内容
我还尝试使用WorksheetFunction.Match(headerText, Range("1:1"), 0),但是出现了同样的问题。

其他注意事项和要求
这是一个插件的一部分,因此,如果不必要,我不想更改用户的Excel工作表中的任何内容(即,我希望删除回车符)。

从技术上讲,我正在使用一个函数进行操作:

Public Function getColumn(headerText As Variant)
    getColumn = Range("1:1").Find(headerText).Column
End Function

谢谢!


1
如果您尝试 Range("1:1").Find("*" & headerText & "*").Column 会发生什么? - BruceWayne
没有运气。同样的问题。 - Alex Silverman
你是如何声明 newCol 的?我输入了 "Test [alt-enter] Test" 并运行了 newCol = Range("1:1").Find("Test").Column,它可以正常工作。 - BruceWayne
你尝试过使用 TrimCLEAN 函数吗? - Elbert Villarreal
如果您绝对确定它在范围(1:1)内,那么您可以尝试使用 Range("1:1").Find(What:=headerText, LookAt:=xlPart).Column - LocEngineer
显示剩余6条评论
3个回答

1
请尝试以下代码。
Public Function getColumn(headerText As String)
    str1 = Split(headerText, vbCrLf)
    str2 = UBound(str1)
    b = Range("1:1").Find(str1(0) & Chr(10) & str1(1)).Column
End Function

请参考BruceWayne上面的评论。这个方法不起作用。这很有道理,因为我有整个字符串,所以通配符“*”并没有什么作用。 - Alex Silverman
这对我来说不是完美的解决方案,但可以作为备用方案。首先,我会确定是否有回车符,如果有,我将使用此代码。谢谢! - Alex Silverman

1
这是一个问题:有换行符和没有换行符的文本不同,因此.Find会失败。你应该进行模式查找。我刚刚测试了一下,只要没有换行符就必须有一个空格,这种方法是有效的。
Sub test()
Dim rex As RegExp, ran As Range
Dim col As Integer, headerText As String

'read you headerText here

Set rex = New RegExp
rex.Pattern = RegexIt(headerText)

For Each ran In Range("1:1")
    If rex.test(ran.Text) Then
        col = ran.Column
        Exit For
    End If
Next ran

MsgBox col

End Sub

Function RegexIt(what As String) As String

what = Replace(what, "(", "\(")
what = Replace(what, ")", "\)")
what = Replace(what, "[", "\[")
what = Replace(what, "]", "\]")
what = Replace(what, "<", "\<")
what = Replace(what, ">", "\>")
what = Replace(what, " ", "[\n ]?")
what = Replace(what, vbCrLf, "[\n ]?")

End Function

祝你好运!

编辑:需要参考微软VBScript正则表达式5.5

编辑2:为了使用变量而进行的编辑。解释:将变量值中的空格替换为可选的空格/换行符,转义括号以进行模式匹配。


谢谢。不幸的是,正如我上面提到的,我需要搜索变量,因为确切的字符串可能会有所不同。你能用带有变量的正则表达式吗? - Alex Silverman
提供的断点只能出现在变量值之间的空格中,是的。参见Edit2。 - LocEngineer
这个看起来更直观,但比Karthick上面的解决方案更笨重。 - Alex Silverman
它看起来确实有点笨重。但是如果没有换行符,Kathick的代码将无法工作。此外,我上面的代码中包含错误。我稍微修改了一下,并且还扩展了一下,以适应头部文本包含空格、换行符或需要在正则表达式中转义的字符的情况。不过应该没问题了。 - LocEngineer

0

即使标题单元格包含回车符,您的代码也应该正常工作:

Sub FindColumnWithTextInRowOne()
    Dim headerText As String, newCol As Long

    headerText = "whatever"
    newCol = Range("1:1").Find(headerText).Column
    MsgBox newCol
End Sub

enter image description here


这是因为您使用的Find()不需要匹配单元格的全部内容。

编辑#1:

如果标题单元格是使用公式构建的,则应使用稍微不同的Find()

   Sub FindColumnWithTextInRowOne()
    Dim headerText As String, newCol As Long, r As Range
    headerText = Range("H1").Text
    newCol = Range("1:1").Find(What:=headerText, LookAt:=xlWhole, LookIn:=xlValues).Column
    MsgBox newCol
End Sub

enter image description here


你正在搜索标题文本的一部分,而不是整个文本。 - Alex Silverman
尝试搜索Range("H1").Value,或者无论您将该文本存储在何处。它不应该起作用。 - Alex Silverman
没有这样的运气,尽管情况稍有不同。因为我正在使用一个表单,headerText实际上是传递给函数的下拉列表/组合框的值。使用LookAt:=xlWholeLookin:=xlValues也没有帮助。 - Alex Silverman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接