2016-11-17 103 views
0

我在下拉列表中不同的名稱(客戶經理),每個客戶經理有1個或超過1「SID」(6個位數)。如果我選擇「A」(客戶經理)誰擁有3組的SID,如果但我希望看到「B」客戶經理的SID誰只有1個SID,所以我希望清除最後的結果,並只顯示對於B客戶經理,但在我的代碼中最後的結果仍然存在,代碼更新'B'的第一個SID並保留其他兩個SID'A'。更新結果(明確最後的結果),如果下拉列表更新

如果我選擇'B',則不需要「A」SID 「(它應該只顯示一個和明確的休息細胞

這是我的代碼:

Sub Discrepancies_by_Acc_Mgr() 

''SIDs are in column 'D' in my Data, 
''Account manager names are in Column 'W', 
'' Column 'Z3' where i have my drop down list, 
'' From Column 'Z6' where i want to start print the SIDs if find in the data 

    s = 6 
    For i = 2 To 25000 
     If (Worksheets("Data").Range("W" & i)) =(Worksheets("Data").Range("Z3")) Then 
      Worksheets("Data").Range("Z" & s) = Worksheets("Data").Range("D" & i) 
      s = s + 1 
     End If 

    Next 

End Sub 

回答

0

結果寫之前就清除單元格的內容:

Sub Discrepancies_by_Acc_Mgr() 
    Dim s As Long 
    Dim i As Long 

    With Worksheets("Data") 
     'Clear existing contents if they exist 
     If Not IsEmpty(.Range("Z6")) Then 
      .Range("Z6:Z" & .Range("Z" & .Rows.Count).End(xlUp).Row).ClearContents 
     End If 

     s = 6 
     For i = 2 To 25000 
      If .Range("W" & i).Value = .Range("Z3").Value Then 
       .Range("Z" & s).Value = .Range("D" & i).Value 
       s = s + 1 
      End If 
     Next 
    End With 
End Sub