VBA/Macro 添加另一列条件

VBA/Macro to add another colum with conditions

我需要帮助在 M 列中编写额外的列或 header 名为 "Person" 的条件:

如果第一列(Column A)有这些关键字"AU", "FJ", "NC", "NZ", "SG12",(column M)中的文本应该是Person1

如果第一列(Column A)有这些关键字"ID", "PH26", "PH24", "TH", "ZA",column M)中的文本应该是Person2

如果第一列(Column A)有这些关键词"JP", "MY", "PH", "SG", "VN",(column M)中的文本应该是Person3

我想让这个动作成为最后一件事(在一切之后)。

我试着录制了一个宏。过滤关键字然后手动输入然后向下滑动复制但是过滤后的数据似乎应该有另一种粘贴方式。

范围也应该是动态的,因为我每次都会有不同数量的数据

下面是我目前的代码:

Sub person()

    Selection.AutoFilter
    ActiveSheet.Range("$A:$L").AutoFilter Field:=1, Criteria1:=Array("AU", _
        "FJ", "NC", "NZ", "SG12"), Operator:=xlFilterValues
    ActiveWindow.LargeScroll ToRight:=1
    Range("M2").Select
    ActiveCell.FormulaR1C1 = "Person1"
    Selection.FillDown
End Sub

试试下面的代码

Sub testing()
    last = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To last
        If Cells(i, 1) = """AU"", ""FJ"", ""NC"", ""NZ"", ""SG12""," Then
            Cells(i, 13) = "Person1"
        ElseIf Cells(i, 1) = """ID"", ""PH26"", ""PH24"", ""TH"", ""ZA"","  Then
            Cells(i, 13) = "Person2"
        ElseIf Cells(i, 1) = """JP"", ""MY"", ""PH"", ""SG"", ""VN""," Then
            Cells(i, 13) = "Person3"
        [..]
        End If
    Next i
End Sub

带 OR 的嵌套 IF 公式可以完成此操作。

With Worksheets("Sheet1")   '<~~ you should know what worksheet you are on!
    With Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp))
        .Offset(0, 12).FormulaR1C1 = _
            "=if(or(rc1={""AU"", ""FJ"", ""NC"", ""NZ"", ""SG12""}), ""Person1"", " & _
             "if(or(rc1={""ID"", ""PH26"", ""PH24"", ""TH"", ""ZA""}), ""Person2"", " & _
             "if(or(rc1={""JP"", ""MY"", ""PH"", ""SG"", ""VN""}), ""Person3"", " & _
             "TEXT(,))))"
        'optionally revert the formulas to values
        '.Offset(0, 12) = .Offset(0, 12).value
    End With
End With