简体   繁体   中英

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. Where would you start with this?

在此处输入图片说明

Without a macro:

In B2 enter:

=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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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