gpt4 book ai didi

vba - Range.Find() 带有回车符的文本 Excel VBA

转载 作者:行者123 更新时间:2023-12-02 19:31:14 25 4
gpt4 key购买 nike

我正在尝试做什么
找到标题单元格包含唯一字符串的列。换句话说,我知道该单元格的文本,并且我知道该单元格位于第 1 行,但我不知道在哪一列。注意:我想搜索整个文本,而不仅仅是其中的一部分。注意2:文本可能会有所不同,因此我无法将硬编码到我的代码中。相反,我需要使用存储值的变量

问题
当标题文本中没有回车符时,简单的 newCol = Range("1:1").Find(headerText).Column 工作正常。但是,如果有回车符,这就不起作用了。它会抛出错误“未设置对象变量或 With block 变量”。这是我的确切标题字符串:

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

谢谢!

最佳答案

请尝试使用以下代码

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

关于vba - Range.Find() 带有回车符的文本 Excel VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36747392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com