更新列表中具有重复值的列和行值

时间:2016-05-02 13:33:18

标签: excel vba excel-vba

我已经合并了可变数量的工作表,这些工作表通过此代码包含“数据”一词;

  Dim masterSheet As Worksheet

 Set masterSheet = Sheets("Komko")

 'Variable to save the used Range of the master sheet
  Dim usedRangeMaster As Integer

 Dim ws As Worksheet

  'loop through each worksheet in current workbook
 For Each ws In Worksheets

    'If sheetname contains "data" (UCase casts the Name to upper case    letters)
    If InStr(1, UCase(ws.Name), "DATA", vbTextCompare) > 0 Then

    'calculate the used range of the master sheet
    usedRangeMaster = masterSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1

    'Variable to save the used Range of the sub sheet
    Dim usedRangeSub As Integer

    'calculate the used range of the sub sheet
    usedRangeSub = ws.UsedRange.SpecialCells(xlCellTypeLastCell).Row

    'copy relevant range from the subsheet
    ws.Range("C1:C" & usedRangeSub).Copy

    'paste the copied range after the used range in column a
    masterSheet.Range("A" & usedRangeMaster).PasteSpecial
End If
Next ws

此代码复制包含表格的“数据”的C列,然后将其粘贴到我的主表格(称为“Komko”)列A.现在我想在C列中标识相同的值并删除完整的上一个匹配值的行。 (例如,如果C1的值为“123”,如果C7的值为“123”,则包含C1的整行应自动删除)

我该怎么做?

1 个答案:

答案 0 :(得分:0)

请参阅http://analysistabs.com/vba/find-duplicate-values-column/以查找重复的条目,并Delete a row in Excel VBA了解如何以良好的方式删除该行。

合并列C后开始搜索。如有必要,使用布尔变量跳过第一个找到的条目。从第一个链接替换代码Cells(iCntr, 2) = "Duplicate",并将删除行的方式放在那里。