VBA - 条件值替换

时间:2016-04-08 14:01:19

标签: excel vba excel-vba

我有一个电子表格,其中包含多列“A:Z”中的值我希望在列" B"中找到并替换值取决于" B"列中的值和专栏" C"。例如:

如果B列= SWI且C列= IN,则将SWI替换为Switch Subscription

但如果B列= SWI且C列= OUT,则将SWI替换为Switch Redemption

表格如下:

Col B       Col C
RED         OUT
RED         OUT
SWI         IN
SWI         OUT
SWI         IN
SWI         OUT
SUB         IN

我试着编写代码,但它没有用!问题在哪里?

N = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 1 To N
        v1 = Cells(i, 2).Value
        v3 = Cells(i, 3).Value
        If v1 = "SWI" And v3 = "IN" Then Cells(i, 2).Value = "Switch Subscription"
        If v1 = "SWI" And v3 = "OUT" Then Cells(i, 2).Value = "Switch Redemption"
    Next i

2 个答案:

答案 0 :(得分:0)

尝试以下代码

    N = Cells(Rows.Count, "B").End(xlUp).Row
    For i = 1 To N
        v1 = Cells(i, 2).Value
        v3 = Cells(i, 3).Value
        If v1 = "SWI" And v3 = "IN" Then Cells(i, 2).Value = "Switch Subscription"
        If v1 = "SWI" And v3 = "OUT" Then Cells(i, 2).Value = "Switch Redemption"
    Next i

答案 1 :(得分:0)

你有两个if语句,但从未添加End If试试这个

If v1 = "SWI" And v3 = "IN" Then Cells(i, 2).Value = "Switch Subscription"
ElseIf v1 = "SWI" And v3 = "OUT" Then Cells(i, 2).Value = "Switch Redemption"
End If