Excel VBA 查找范围内的第一个值行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28761319/
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 VBA find first value row in range
提问by Macb3th
I have this range it selects all values in that column:
我有这个范围,它选择该列中的所有值:
Set c = ThisWorkbook.Worksheets("SplitSkillInterv").Range("C3:C" & Rows.Count)
Set c = ThisWorkbook.Worksheets("SplitSkillInterv").Range("C3:C" & Rows.Count)
Now I need to make it to find some value and then its row (it could be 1 or 10+, so I need to find first one) any ideas?
现在我需要让它找到一些值然后它的行(它可能是 1 或 10+,所以我需要找到第一个)有什么想法吗?
I need to find for example: in range c values are from 1 to 10 and randomly repeating till total 100 rows. I and lets say i need to find when first time will be number 3 in range and its row.
例如,我需要找到:范围内的 c 值从 1 到 10 并且随机重复直到总共 100 行。我可以说我需要找到第一次是范围及其行中的第 3 次。
采纳答案by FreeMan
I'm also a bit unclear what you're asking, but using Find
might do the trick for you:
我也有点不清楚你在问什么,但使用Find
可能对你有用:
With ThisWorkbook.Worksheets("SplitSkillInterv")
Set Rng = .Range("C3:C" & RowCount).Find(What:=".", LookIn:=xlValues, lookat:=xlPart, _
MatchCase:=False) 'find the first "." in the first row
While Not Rng Is Nothing
'Your stuff here
Set Rng = .Range("C3:C" & RowCount).Find(What:=".", LookIn:=xlValues, _
lookat:=xlPart, MatchCase:=False) 'find the next "."
Wend
End With
I would define RowCount
prior to entering this code segment. Replace What:="."
with whatever it is you're looking for.
我会RowCount
在输入此代码段之前定义。替换What:="."
为您正在寻找的任何内容。
Rng
will then be the cell that contains whatever you're searching for. You can do your stuff to that cell at 'Your stuff here
, then the next line will find the next occurrence of your search item. If you only need to do something with the first item, eliminate the rows of code after Set Rng = ...
Rng
然后将是包含您正在搜索的任何内容的单元格。您可以在 处对该单元格进行操作'Your stuff here
,然后下一行将找到下一次出现的搜索项。如果你只需要对第一项做一些事情,去掉后面的代码行Set Rng = ...
回答by Erik Blomgren
The question is a bit unclear to me but they way I understand it this would get you the row number of the first row with the value you want to find, if you replace 'Something' with the value you want to find.
这个问题对我来说有点不清楚,但他们的方式我理解它会让你得到第一行的行号和你想要找到的值,如果你用你想要找到的值替换“Something”。
i = 3
ValueToFind = 'Something'
Do Until ValueToFind = ThisWorkbook.Worksheets("SplitSkillInterv").Cells(i, 3).Value
i = i + 1
Loop
FirstRowWithValue = i