与给定范围地址的同一行中查找字符串值

发布于 2025-02-03 07:43:34 字数 1096 浏览 1 评论 0原文

我有一个搜索,可以通过有效的特定标头标识一个范围地址。

我正在处理的功能需要在同一行中搜索另一个标头字符串并返回单元格地址。

由于某种原因,查找函数不会在文件中使用标头的地址填充范围变量。

我尝试调整查找功能中的参数,但我没有任何运气来解决这个问题。

事先感谢您的帮助!

Private Sub FindAllHeadersInRow(dataFile As String, rng As String, headerName As String)
MsgBox "Finding all headers in the selected row"
'search left in the given row to find other header to copy data from under
Dim HeaderColFoundRng As Range
Dim dataWB As Workbook
Set dataWB = Workbooks.Open(dataFile)
Set HeaderColFoundRng = dataWB.Sheets("Sheet1").Range(rng).Find(What:=headerName, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchDirection:=xlPrevious) 'or xlNext
    'needs to set each value address to a range that will be sent out
    
    MsgBox HeaderColFoundRng.Address  '<<< repeated variable not set error!
        
End Sub

Sub testSub2()
Dim FileToOpen As String
FileToOpen = Application.GetOpenFilename(Title:="Select Data file")
Call FindAllHeadersInRow(FileToOpen, "A68:AZ68", "Transaction Date") 'Header needed is located in T68
End Sub

I have a search that Identifies a range address with a specific header that works.

The function I am working on needs to search in the same row for another header string and return the cell address.

For some reason the find function does not populate the range variable with the address of the header in the file.

I have tried adjusting parameters in the find function but I am not having any luck figuring this out.

Thanks in advance for your help!

Private Sub FindAllHeadersInRow(dataFile As String, rng As String, headerName As String)
MsgBox "Finding all headers in the selected row"
'search left in the given row to find other header to copy data from under
Dim HeaderColFoundRng As Range
Dim dataWB As Workbook
Set dataWB = Workbooks.Open(dataFile)
Set HeaderColFoundRng = dataWB.Sheets("Sheet1").Range(rng).Find(What:=headerName, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchDirection:=xlPrevious) 'or xlNext
    'needs to set each value address to a range that will be sent out
    
    MsgBox HeaderColFoundRng.Address  '<<< repeated variable not set error!
        
End Sub

Sub testSub2()
Dim FileToOpen As String
FileToOpen = Application.GetOpenFilename(Title:="Select Data file")
Call FindAllHeadersInRow(FileToOpen, "A68:AZ68", "Transaction Date") 'Header needed is located in T68
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文