搜索B列中的文本,如果为true,则搜索其他文本到L列

时间:2019-04-29 12:23:18

标签: excel vba

我需要在B:B列中搜索特定文本,然后如果为true,则将其他文本粘贴到L:L列,例如:

enter image description here

Sub teste()
    Application.ScreenUpdating = False

    last = Cells(Rows.Count, "B").End(xlUp).Row

    For i = last To 1 Step -1    
        If (Cells(i, "B").Value) = "string_1" Then    
            Range("L2").Select
            ActiveCell.FormulaR1C1 = "some_text_1"

            'LastRow = Range("A" & Rows.Count).End(xlUp).Row
            'Range("L2").AutoFill Destination:=Range("L2:L" & LastRow)
        End If
    Next i
End Sub

我只能粘贴第一个文本(如果为true)或在L:L列中填充相同的文本。

2 个答案:

答案 0 :(得分:0)

你的意思是那样的吗?

  • 如果B列为string_1,则将C列复制到L列

For i = last To 1 Step -1    
    If (Cells(i, "B").Value) = "string_1" Then    

        'copy value from C to L
        Cells(i, "L").Value = Cells(i, "C").Value

    End If
Next i

您可能会受益于阅读 How to avoid using Select in Excel VBA

答案 1 :(得分:0)

Sub teste()
Application.ScreenUpdating = False

s1 = "first_text"
s2 = "second_text"
s3 = "third_text"

last = Cells(Rows.Count, "B").End(xlUp).Row

For i = last To 1 Step -1

If (Cells(i, "B").Value) = "string_1" Then
    Cells(i, "L").Value = s1
ElseIf (Cells(i, "B").Value) = "String_2" Then
    Cells(i, "L").Value = s2
ElseIf (Cells(i, "B").Value) = "string_3" Then
    Cells(i, "L").Value = s3
End If

Next i

End Sub