Excel VBA 在工作表上查找具有特定值(字符串)的单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35208190/
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-08 10:12:30  来源:igfitidea点击:

Excel VBA find a cell on worksheet with specific value (string)

excelexcel-vbaexcel-2010vba

提问by acr_scout

I need help finding a text value on a worksheet and offset from there two rows down.

我需要帮助在工作表上查找文本值并从那里向下偏移两行。

I have a cell with "Bottom of Range" title in it. Two cells below that is the actual value. The issue is "Bottom of Range" could be in 7 possible places depending on the worksheet template.

我有一个带有“范围底部”标题的单元格。下面的两个单元格是实际值。问题是“范围底部”可能位于 7 个可能的位置,具体取决于工作表模板。

How can I find the cell with "Bottom of Range" in it? I can then .Offset(2,0).value2 to get what I want.

如何找到带有“范围底部”的单元格?然后我可以 .Offset(2,0).value2 得到我想要的。

In the grander scheme of things. I am cycling through hundreds of workbooks and then through each of their worksheets collecting data and centralizing it. Because the values I am looking for could be anywhere I need to look for their title/definition before getting to the values.

在更宏大的计划中。我循环浏览数百个工作簿,然后浏览他们的每个工作表,收集数据并将其集中起来。因为我正在寻找的值可能在我需要在获取值之前查找其标题/定义的任何地方。

回答by BruceWayne

Here you go! You want to use .Find:

干得好!你想使用 .Find:

Sub t()
Dim bottomCell As Range
Dim offsetCell As Range
With Sheets("Sheet1")
    Set bottomCell = .Cells.Find(what:="Bottom of Range")
    Set offsetCell = bottomCell.Offset(2, 0)
    ' Now, your offsetCell has been created as a range, so go forth young padawan!
End With
End Sub