vba Excel宏在列中查找第一个对应值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19378395/
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
Excel macro find first corresponding value in column
提问by JJJP
I am quite new with macros and hopeing thet somone can help me with this. It would be highly appriciated.
我对宏很陌生,希望有人能帮我解决这个问题。这将是高度appriciated。
This is what I am trying to do.
这就是我想要做的。
I have a cell with a value in sheet 1, lets say it is cell C2 and it is equal to 1234
我有一个在工作表 1 中有一个值的单元格,假设它是单元格 C2,它等于 1234
I have a column with values in sheet 2, lets say it is column B and it has values (3333,1234,1234,1234,8897,8897,6675)
我在表 2 中有一个带有值的列,假设它是 B 列并且它具有值 (3333,1234,1234,1234,8897,8897,6675)
I want the macro to go through column B in sheet2 and find the firstcell that corresponds to the cell value in cell C2 sheet1. Then I want to place the marker to steps to the right.
我希望宏通过 sheet2 中的 B 列并找到与单元格 C2 sheet1 中的单元格值对应的第一个单元格。然后我想把标记放在右边的台阶上。
In the example above. C2 value corresponds to B1 (even though there are more, this is the first) and then the marker is placed in B3. Please observe that I want to stop when the first corresponding value is found.
在上面的例子中。C2 值对应于 B1(即使有更多,这是第一个),然后将标记放置在 B3 中。请注意我想在找到第一个对应值时停止。
Thank you in advance! :)
先感谢您!:)
/J
/J
回答by Fewster
I've assume "to steps to the right" as two (2) steps to the right. Hope this helps. As ever with macro records there's a lot of stuff you might not need there.
我假设“向右走”是向右走两 (2) 步。希望这可以帮助。与以往一样,宏记录中有很多你可能不需要的东西。
rngFound = Sheet2.Columns("B:B").Find(What:=Sheet1.Range("C2"), _
After:=Range("B1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Address
Range(rngFound).Offset(0, 2).Select
This can be tidied up a bit if you'd like.
如果您愿意,可以稍微整理一下。