VBA /macros添加条件的另一个列

我需要帮助来编写列M中名为“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

我希望这个行动成为最后一件事(在所有事情之后)。

我试图录制一个macros。 过滤关键字,然后手动input,然后向下滑动复制,但似乎应该有另一种方式来粘贴过滤的数据。

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

以下是我的代码到目前为止:

 Sub person() Selection.AutoFilter ActiveSheet.Range("$A$1:$L$38").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 

带有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 

尝试下面的代码

 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