2013-11-21 52 views
1

我有以下數據Excel VBA中刪除已經對於給定的索引

Name  ID Value 

Alice 12C 500 

Bob  14  60 

Dan  15C 64 

Dan  1C 25 

Alice 4  556 

Bob  11  455 

在我的數據中,Alice具有兩個數值(4)和串+數字ID(12C)和欲混合值的行刪除所有的Alice行,而我想要堅持其ID是嚴格數字(Bob 11,14)或嚴格字符串+數字(Dan 15C,1C)的名稱的數據。

首先是讓獨特的名稱項的數組:

FinalRow = 7 
    Name_column = 1 
    n = 1 

    Dim FID_Array() As Variant 

      ReDim Preserve FID_Array(1 To 1) 
      FID_Array(1) = Cells(2, Name_column) 

    For j = 3 To FinalRow 

     If Cells(j, Name_column).Value <> FID_Array(n) Then 
       ReDim Preserve FID_Array(1 To n + 1) 
       FID_Array(n + 1) = Cells(j, Name_column).Value 
       n = n + 1 


     End If 
    Next j 

然後我讓包含特定名稱

ReDim Preserve Count_FID_Array(1 To 1) As Variant 
    n = 1 
    range_FID = A2:A7 


' In my actual code this is Range_FID 
' range_FID = Cells(2, FolderId_column).Address & ":" & Cells(FinalRow, FolderId_column).Address 

    For Each itm5 In FID_Array() 

      Count_FID_Array(n) = Application.CountIf(" & range_FID & ", " & itm5 & ") 
      ReDim Preserve Count_FID_Array(1 To n + 1) 
      n = n + 1 

    Next itm5 

我不認爲我的COUNTIF是行號的數組加工。我試圖將Count_FID_Array的值存儲在另一個單元格中的另一個單元格中,但我得到#value!

如果我得到countIf工作,然後我要按名稱對數據進行排序,然後雙循環檢查ID變量下一個「n」次,以查看最後一位數字是否爲「C」或者檢查ID是否爲所有的數字。

請你指出爲什麼我的countif不工作,是否有更聰明的方法來做到這一點?

我在這裏使用名稱數組,因爲最後我想將數組提供給自動篩選器並刪除我不想要的行。

更新1 3:45 PM 2013年11月21日:我已經解決了這個如下:

我基本上創建了三列。第一列是0或1,具體取決於ID是否都是數字。第二列是0或1,取決於最後一位數字是「C」(在我的實際工作中最後兩位數字是「IB」),最後我將這些出現的頻率與名稱本身的頻率進行比較。如果其中任何一個匹配,那麼我給它的數字1其他0.我稍後使用此索引來自動篩選。

現在我將嘗試在VBA代碼中使用zx8754的較短公式,並嘗試解決Joe提出的有關Countif的問題。

子conditionsforsubfolders()

FinalColumn = Cells(1, Columns.Count).End(xlToLeft).Column 
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 
    ActiveWorkbook.ActiveSheet.Columns(FinalColumn + 1).Insert 
    ActiveWorkbook.ActiveSheet.Columns(FinalColumn + 2).Insert 
    ActiveWorkbook.ActiveSheet.Columns(FinalColumn + 3).Insert 

    Isnumber_Column = FinalColumn + 1 
    Is_IB_Column = FinalColumn + 2 
    Exceptions_Column = FinalColumn + 3 
    Cells(1, Isnumber_Column) = "Number" 
    Cells(1, Is_IB_Column) = "Letters" 
    Cells(1, Exceptions_Column) = "Exceptions" 

    For j = 1 To FinalColumn 
    If Cells(1, j).Value = "TradeId" Then 
     TradeId_column = j 
    ElseIf Cells(1, j).Value = "Total Notional per folder" Then 
     Total_Notional_Per_Folder_Column = j 
    ElseIf Cells(1, j).Value = "ExternalId" Then 
     ExternalId_Column = j 
     ElseIf Cells(1, j).Value = "FolderId" Then 
     FolderId_column = j 

    End If 
    Next j 
    range_FolderId_fixed = Cells(2, FolderId_column).Address & ":" & Cells(FinalRow, FolderId_column).Address 
    range_TradeId_fixed = Cells(2, TradeId_column).Address & ":" & Cells(FinalRow, TradeId_column).Address 
    range_Isnumber = Cells(2, Isnumber_Column).Address & ":" & Cells(FinalRow, Isnumber_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False) 
    range_Isnumber_fixed = Cells(2, Isnumber_Column).Address & ":" & Cells(FinalRow, Isnumber_Column).Address 
    range_Is_IB = Cells(2, Is_IB_Column).Address & ":" & Cells(FinalRow, Is_IB_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False) 
    range_Is_IB_fixed = Cells(2, Is_IB_Column).Address & ":" & Cells(FinalRow, Is_IB_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False) 
    range_FolderId_cell = Cells(2, FolderId_column).Address(RowAbsolute:=False, ColumnAbsolute:=False) 
    range_TradeId_cell = Cells(2, TradeId_column).Address(RowAbsolute:=False, ColumnAbsolute:=False) 
    range_Exceptions = Cells(2, Exceptions_Column).Address & ":" & Cells(FinalRow, Exceptions_Column).Address(RowAbsolute:=False, ColumnAbsolute:=False) 


    Range(range_Isnumber).Formula = "=Isnumber(" & range_TradeId_cell & ")*1" 
    Range(range_Is_IB).Formula = "=(RIGHT(" & range_TradeId_cell & ",2)= ""IB"")*1" 

    Range(range_Exceptions).Formula = "=(SUMIF(" & range_FolderId_fixed & "," & range_FolderId_cell & "," & range_Isnumber_fixed & ")= COUNTIF(" & range_FolderId_fixed & "," & range_FolderId_cell & "))*1 +(SUMIF(" & range_FolderId_fixed & "," & range_FolderId_cell & "," & range_Is_IB_fixed & ")= COUNTIF(" & range_FolderId_fixed & "," & range_FolderId_cell & "))*1 " 
    Worksheets("Sheet1").UsedRange.AutoFilter Field:=7, Criteria1:="=1" 

末次

回答

1

的問題,您的通話CountIF是你傳遞一個不良形成的字符串。你真的會傳遞「range_FID & ", " & itm5」。

首先,你設置爲正確定義range_fid:

Dim range_fid As Range 
Set range_fid = [A2:A7] 

呼叫COUNTIF有:

count_fid_array(n) = Application.WorksheetFunction.CountIf(range_fid, itm5) 

雖這麼說,我想不同的去了解它:

Dim c As Range 
Dim people As Collection: Set people = New Collection 
Dim person As Collection 
Dim code As String 

For Each c In Range(Range("a2"), Range("a2").End(xlDown)) ' loop through all rows 
    If IsNumeric(c.Offset(0, 1)) Then ' check if the ID is numeric or not 
     code = "num" 
    Else 
     code = "alphanum" 
    End If 

    On Error Resume Next ' Needed in order to avoid error when person already exists in collection 
    Set person = New Collection 
    person.Add c.Value, "name" 
    person.Add code, "code" 
    people.Add person, c.Value ' will only be added if name doesn't already exist in collection 
    On Error GoTo 0 
    If people(c.Value)("code") <> code Then ' if the format (alpha/num) of the ID on the current row is different than the format of a previous row for this name.... 
     people(c.Value).Remove ("code") ' then set the code to "diff" 
     people(c.Value).Add "diff", "Code" 
    End If 
Next 

For Each person In people ' just display the content; you can take appropriate action here 
    Debug.Print person("name") & ": " & person("code") 
Next 

結果是一個包含名稱和每個代碼的集合。該代碼將是一個:

  • NUM:一個名字的所有值是數字(BOB)
  • alphanum:一個名字的所有值都是字母數字(丹)
  • 差異 :名稱至少有一個數字和字母數字(愛麗絲)

請注意,這可以做一個更清晰的字典,而不是一個集合,或無線一個班,但我選擇採取最直接的方法。

+0

謝謝你爲一個偉大的答案。你給了我很多想法和學習。我需要一點時間才能回答你一些問題。儘管謝謝! – Amatya

2

式的解決方案,沒有VBA:

=IF(SUMPRODUCT(--($A$2:$A$7=A2),--(ISNUMBER($B$2:$B$7)))=1,"delete","keep")

enter image description here