简体   繁体   English

Excel:连续查找VBA最右边(最后)出现的值

[英]Excel: Find most right (last) occurrence of a value with VBA in a row

In an Excel sheet I want to find, using VBA, the rightmost occurrence of a value in a specific row. 在Excel工作表中,我想使用VBA查找特定行中最右边的值。

Example: Say I have the following values in row 3 (cells delimited by spaces in this example): 示例:假设我在第3行中有以下值(在此示例中由空格分隔的单元格):

1 2 1 5 1 5 7 1 4 5 6  

Now I want to get the column number of the rightmost occurrence of the value 1 . 现在我想得到值1的最右边出现的列号。 The answer is 8. 答案是8。

How do I get this answer in an efficient way and with a single command? 如何以有效的方式并使用单个命令获得此答案? Can i do this with Range.Find() ? 我可以用Range.Find()做到这一点吗?

Yes you can: 是的你可以:

range("3:3").Find(what:=1,searchorder:=xlbycolumns,searchdirection:=xlPrevious).column

if you know there will be at least one occurrence of 1 (otherwise you should test the result range before trying to refer to its column) 如果你知道至少有一次出现1(否则你应该在尝试引用它的列之前测试结果范围)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM