简体   繁体   English

如果满足条件,将值复制到另一个单元格的宏

[英]Macro to copy a value into another cell if a criteria is met

I need to write a macro that for example, would copy the value from column A where the condition of cheese was met, and then pasted this value into the adjacent cell in the next column. 我需要编写一个宏,例如,它将从满足奶酪条件的A列复制值,然后将该值粘贴到下一列的相邻单元格中。 Where would you start with this? 您将从何处开始?

在此处输入图片说明

Without a macro: 没有宏:

In B2 enter: B2中输入:

=IF(A2="CHEESE",A2,"")

and copy down 然后抄下来

With a macro: 使用宏:

Sub Cheesey()
    For Each r In Intersect(ActiveSheet.UsedRange, Range("A:A"))
        If r.Text = "CHEESE" Then
            r.Offset(0, 1) = "CHEESE"
        End If
    Next r
End Sub

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

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