2016-04-01 80 views
1

我需要幫助編寫額外的列或標題的M列名爲「Person」的條件:VBA /宏再添科拉姆與條件

如果第一列(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$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 

回答

1

一與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 
+0

解決了我的問題!感謝這個答案 – markerbean

1

嘗試下面的代碼

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 
+0

現在解決了我的問題。謝謝! – markerbean