调用remove duplicates方法后返回剩余值的计数

时间:2015-07-24 06:42:50

标签: excel vba excel-vba

我有几个摘要表,我试图将数据从一个到另一个自动化,代码目前都在运行,但是当我重新分配&#34时; Tier2"命名范围我使用xlDownxlRight,对我来说感觉有点草率。

我注意到,当您手动删除重复项时,它会为您提供一个msgbox,其中包含剩余值的计数。

有没有办法让remove duplicates方法也将此属性返回给变量?

'copies the first two columns of the Tier3 table
Range([Tier3_anchor], WorksheetFunction.Index([Tier3], r, c)).Copy

'Pastes as values into the Tier2 table
Range("Tier2_Anchor").PasteSpecial xlPasteValues

'removes duplicates from the Tier2 table
Range([Tier2_anchor], Cells([Tier2_anchor].Row + r - 1, [Tier2_anchor].Column + 1)).RemoveDuplicates Columns:=2, Header:=xlNo  

'This is a rough way to define the range of the Tier2 Table, there is a messagebox that returns remaining values after the remove duplicates, maybe able to capture that!   
Range("Tier2_anchor", Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Name = "Tier2"  

理想情况下我会做这样的事情:

Dim r As Integer
Dim c As Integer
r = UBound(A, 1)
c = UBound(A, 2)

'pastes the array into the workbook with the top left corner starting on the named range "Tier3_Anchor"
Range([Tier3_anchor], Cells(r + [Tier3_anchor].Row, c + [Tier3_anchor].Column)).Value = A
'renames the pasted data to Tier3
Range([Tier3_anchor], Cells([Tier3_anchor].Row + r, [Tier3_anchor].Column + c)).Name = "Tier3"  

1 个答案:

答案 0 :(得分:2)

您可以使用=COUNTA()工作表功能吗?

'removes duplicates from the Tier2 table
Range([Tier2_anchor], Cells([Tier2_anchor].Row + r - 1, [Tier2_anchor].Column + 1)).RemoveDuplicates Columns:=2, Header:=xlNo  

'// Use CountA to get remaining values:
remainingValues = WorksheetFunction.CountA(Range([Tier2_anchor], Cells([Tier2_anchor].Row + r - 1, [Tier2_anchor].Column + 1)))

MsgBox remainingValues & " values remaining"