如何将标识值导出到新工作表?

时间:2017-04-19 17:23:53

标签: excel vba excel-vba

这是我之前提出的问题的跟进here

我只想快速回顾一下这张表:

ID  Age Grade
1   14  90
2   15  78
3   14  90
4   16  86
5   16  86
6   15  89
7   14  88

我在新表中的所需输出表是:

ID  Age Grade
1   14  90
3   14  90
4   16  86
5   16  86

我使用以下方法检查了B列 AND 列C中具有重复值的行:

Sub Export()

Dim lastRowcheck As Long, n1  As Long

With Worksheets("Sheet1")
    lastRowcheck = Application.Max(.Range("B" & .Rows.Count).End(xlUp).Row, _
                                   .Range("C" & .Rows.Count).End(xlUp).Row)

    For n1 = lastRowcheck To 1 Step -1
        If Application.CountIfs(.Columns("B"), .Cells(n1, "B").Value2, .Columns("C"), .Cells(n1, "C").Value2) > 1 Then
           Debug.Print .Cells(n1, "A") & ":" & .Cells(n1, "B") & ":" & .Cells(n1, "C")
           '''export to new sheet
        End If
    Next n1
End With

End Sub

现在我只需要弄清楚如何将这些行导出到新工作表中,我不知道从哪里开始。

2 个答案:

答案 0 :(得分:2)

更新了您的代码,以显示如何将找到的行导出到新工作表:

@MessageDriven(
  name = "foo",
  activationConfig = {
     @ActivationConfigProperty(propertyName = "resourceAdapter", propertyValue = "activemq-rar-x-x-x"),
:
:

答案 1 :(得分:1)

为什么要按降序使用循环

For n1 = lastRowcheck To 1 Step -1

For n1 = 1 To lastRowcheck

虽然您需要按照数据的顺序获得结果,但可以使用它。

Sub Export()
Dim lastRowcheck As Long, n1  As Long, i As Long
Dim ws As Worksheet
Set ws = Sheets("NewSheet") 'sheet name to export data
i = 2 'add data from row 2 in new sheet
With Worksheets("Sheet1")
lastRowcheck = Application.Max(.Range("B" & .Rows.Count).End(xlUp).Row, _
                               .Range("C" & .Rows.Count).End(xlUp).Row)
For n1 = 1 To lastRowcheck
    If Application.CountIfs(.Columns("B"), .Cells(n1, "B").Value2, .Columns("C"), .Cells(n1, "C").Value2) > 1 Then
       Debug.Print .Cells(n1, "A") & ":" & .Cells(n1, "B") & ":" & .Cells(n1, "C")
       '''export to new sheet
       ws.Cells(i, "A") = .Cells(n1, "A")
       ws.Cells(i, "B") = .Cells(n1, "B")
       ws.Cells(i, "C") = .Cells(n1, "C")
       i = i + 1
    End If
Next n1
End With

End Sub