不是最直接的路线,但是您可以在B和C之间插入宏。然后在该列中转储一个公式,这个公式就是重要的。
喜欢的东西= COUNTIFS(B:B,B:B)这会给你多少次的纪录显示,你就可以将脚本设置为循环删除任何行计数如该值为1
喜欢的东西
Sub Duplicates()
Columns("B:B").Insert Shift:=xlToRight ' inserts a column after b
count = Sheet1.Range("B:B").Cells.SpecialCells(xlCellTypeConstants).count ' counts how many records you have
crange = "C1:C" & count ' this defines the range your formula's go in if your data doesn't start in b1, change the c1 above to match the row your data starts
Sheet1.Range(crange).Formula = "=countifs(B:B,B:B)" ' This applies the same forumla to the range
ct=0
ct2=0 'This section will go cell by cell and delete the entire row if the count value is 1
Do While ct2 < Sheet1.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
For ct = 0 To Sheet1.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
If Sheet1.Range("C1").Offset(ct, 0).Value > 1 Then
Sheet1.Range("C1").Offset(ct, 0).EntireRow.Delete
End If
Next
ct2 = ct2 + 1
Loop
Sheet1.Columns("B:B").EntireColumn.delete
end sub
代码是不漂亮,但它应该做的工作。
* *每个注释更新代码
Sub Duplicates()
Columns("C:C").Insert Shift:=xlToRight ' inserts a column after b
count = Activesheet.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count ' counts how many records you have
crange = "C1:C" & count ' this defines the range your formula's go in if your data doesn't start in b1, change the c1 above to match the row your data starts
Activesheet.Range(crange).Formula = "=countifs(B:B,B:B)" ' This applies the same forumla to the range
ct=0
ct2=0 'This section will go cell by cell and delete the entire row if the count value is 1
'''''
Do While ct2 < Activesheet.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
For ct = 0 To Activesheet.Range("C:C").Cells.SpecialCells(xlCellTypeConstants).count
If Activesheet.Range("C1").Offset(ct, 0).Value = 1 Then
Activesheet.Range("C1").Offset(ct, 0).EntireRow.Delete
End If
Next
ct2 = ct2 + 1
Loop
ActiveSheet.Columns("C:C").EntireColumn.delete
end sub
你可以尝试更新的代码,用Do循环的部分是什么将删除的每一列,我固定它删除任何行,所述计数1.
根据我的理解,你的数据应该在B列,计数应该在C列。如果这是不正确的,更新公式的匹配
必须更改列(“B:B”)。将Shift:= xlToRight插入列(“C:C”)插入Shift:= xlToRight和I当我运行这段代码时,得到一些未找到的单元格错误。我也将sheet1更改为activeSheet。现在我只是用它来将计数放在右侧的列中。如果我能得到它删除列1和按列B排序我会被设置! – chrisrth 2012-02-01 14:21:36
不错。救星。 – chrisrth 2012-02-02 02:02:19