在具有特定值的列中查找所有重复项,并从下一列返回值

时间:2020-01-30 13:01:24

标签: excel vba loops

我正在尝试制作看起来像这样的东西:

enter image description here

在右边的表格中,将存在所有唯一记录,这些记录将存储在特定区域中。但是,可能在更多区域中存在一些记录,并且该信息可以从A列和B列的列表中获取。宏应在D列中获取每个唯一记录,并在每次找到A列时在A列中进行搜索。复制B列中的位置/区域,并粘贴到表中唯一记录的旁边。我想我可以通过循环来做到这一点,但是我在下面的代码中创建的内容实际上并没有用。

第二个挑战是,要了解已将位置复制到表中的位置,需要将新找到的位置粘贴到该唯一记录的下一个空闲单元格中。

我知道我的代码有点吓人,但即使只是建议我应该朝哪个方向前进,我也将不胜感激。

Sub searcharea()

    Dim UC As Variant, UCrng As Range, ra As Range

    Set UCrng = Range("F2:F6")

    For Each UC In UCrng

        Set ra = Cells.Find(What:=UC, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

        ra.Offset(0, 1).Copy Destination:=Range("E2")

    Next

End Sub

3 个答案:

答案 0 :(得分:0)

我建议循环遍历所有行(A + B列),例如:

For i = 1 to Rows.Count
'DoStuff
Next i

对于每一行,将A的值复制到D(如果尚未存在)。 您可以访问像这样的值:

Cells(i, "A").Value
Cells(i, "B").Value

要在列中查找值,请see here。如果发现重复,请使用另一个循环检查特定行中的哪一列(E,F,G ...)是第一个空列,并超过该列B的值。

答案 1 :(得分:0)

尝试一下:

Option Explicit

Sub test()

    Dim LastRowA As Long, LastRowD As Long, i As Long, rngColumn As Long
    Dim rng As Range

    With ThisWorkbook.Worksheets("Sheet1")

        LastRowD = .Cells(.Rows.Count, "D").End(xlUp).Row

        .Range("D2:J" & LastRowD).ClearContents

        LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 2 To LastRowA

            LastRowD = .Cells(.Rows.Count, "D").End(xlUp).Row

            Set rng = .Range("D1:D" & LastRowD).Find(.Range("A" & i).Value, LookIn:=xlValues, lookat:=xlWhole)

            If Not rng Is Nothing Then
                rngColumn = .Cells(rng.Row, .Columns.Count).End(xlToLeft).Column
                Cells(rng.Row, rngColumn + 1).Value = .Range("B" & i).Value
            Else
                .Range("D" & LastRowD + 1).Value = .Range("A" & i).Value
                .Range("E" & LastRowD + 1).Value = .Range("B" & i).Value
            End If

        Next i

    End With

End Sub

答案 2 :(得分:0)

我认为这段代码可以满足您的需求。请尝试。

Option Explicit

Sub SortToColumns()
    ' Variatus @STO 30 Jan 2020

    Dim WsS As Worksheet                    ' Source
    Dim WsT As Worksheet                    ' Target
    Dim Rng As Range
    Dim Fn As String, An As String          ' File name, Area name
    Dim Rls As Long
    Dim Rs As Long
    Dim Rt As Long, Ct As Long

    With ThisWorkbook                       ' change as required
        Set WsS = .Worksheets("Sheet1")     ' change as required
        Set WsT = .Worksheets("Sheet2")     ' change as required
    End With

    With WsT
        ' delete all but the caption row
        .Range(.Cells(2, 1), .Cells(.Rows.Count, "A").End(xlUp)).EntireRow.ClearContents
    End With

    Application.ScreenUpdating = False
    With WsS
        ' find last row of source data
        Rls = .Cells(.Rows.Count, "A").End(xlUp).Row

        For Rs = 2 To Rls                   ' start from row 2 (row 1 is caption)
            Fn = .Cells(Rs, "A").Value
            An = .Cells(Rs, "B").Value
            If FileNameRow(Fn, WsT, Rt) Then
                ' add to existing item
                With WsT
                    Ct = .Cells(Rt, .Columns.Count).End(xlToLeft).Column
                    Set Rng = .Range(.Cells(Rt, "B"), .Cells(Rt, Ct))
                End With
                With Rng
                    Set Rng = .Find(An, .Cells(.Cells.Count), xlValues, xlWhole, xlByRows, xlNext)
                End With
                ' skip if Area exists
                If Rng Is Nothing Then WsT.Cells(Rt, Ct + 1).Value = An
            Else
                ' is new item
                WsT.Cells(Rt, "A").Value = Fn
                WsT.Cells(Rt, "B").Value = An
            End If
        Next Rs
    End With
    Application.ScreenUpdating = True
End Sub

Private Function FileNameRow(Fn As String, _
                             WsT As Worksheet, _
                             Rt As Long) As Boolean
    ' Rt is a return Long
    ' return True if item exists (found)

    Dim Fnd As Range
    Dim Rng As Range
    Dim R As Long

    With WsT
        R = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set Rng = .Range(.Cells(2, "A"), .Cells(R, "A"))
        Set Fnd = Rng.Find(Fn, Rng.Cells(Rng.Cells.Count), xlValues, xlWhole, xlByRows, xlNext)

        If Fnd Is Nothing Then
            Rt = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row + 1, 2)
        Else
            Rt = Fnd.Row
            FileNameRow = True
        End If
    End With
End Function