vba Excel中的VBA - 如果循环中只有条件为真,如何获取单元格值

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

VBA in Excel - how to get a cell value if only condition is true in loop

excelvbaloopsif-statement

提问by user3099425

enter image description here

在此处输入图片说明

how to get the only name which is colum B in cloum E when the status is > 0 in Excel VBA, please help out.

如何在Excel VBA中状态> 0时获得cloum E中唯一的名称是colum B,请帮忙。

回答by engineersmnky

Simple Example and should be expanded to fit you needs

简单示例,应该扩展以满足您的需要

Sub CopyName() 
    Dim lcell AS Range
    For Each lcell in Range("$B","$B")
        If Cells(lcell.Row,lcell.Column + 1) > 0 Then
           Cells(lcell.Row,"E").Value = lcell.Value
        End If
    Next lcell 
End Sub

回答by Ashwin Balamohan

In Column E, you can paste the following:

在 E 列中,您可以粘贴以下内容:

=IF(B1>0, A1, "")

=IF(B1>0, A1, "")

If column B has a value greater than 0, it will fill in column A's value. Otherwise, it'll fill in an empty string (i.e. nothing).

如果 B 列的值大于 0,它将填充 A 列的值。否则,它将填充一个空字符串(即什么都没有)。