如何在 VBA 的列中找到大于零的第一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23475919/
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
How do you find the first value greater than zero in a column in VBA?
提问by Snowblack
I know this is easily done outside of VBA but I want to get proficient with VBA as a beginner.
我知道这在 VBA 之外很容易完成,但我想作为初学者精通 VBA。
I have a long column (about 25,000 rows). I need to find the first instance a cell value is greater than zero in that column. Then I need to return the address of that cell to a variable. So far I've been using a loop to no success, any suggestions?
我有一个很长的列(大约 25,000 行)。我需要在该列中找到单元格值大于零的第一个实例。然后我需要将该单元格的地址返回给一个变量。到目前为止,我一直在使用循环但没有成功,有什么建议吗?
回答by Floris
You can easily do the following:
您可以轻松地执行以下操作:
For each c in Range("A1:A25000").Cells
If c.Value > 0 Then
firstValue = c.Value
firstAddress = c.Address
Exit For
End If
Next
MsgBox "The first value greater than zero is in cell " & firstAddress & _
"; - it has value " & firstValue
回答by engineersmnky
This should work
这应该工作
Function not_zero() AS String
Dim lcell as Range
For Each lcell in Range("$YOUR COLUMN LETTER","$YOUR COLUMN LETTER$LAST ROW")
If CLng(lcell.value) > 0 then
not_zero = lcell.Address
Exit For
End If
Next lcell
End Function